I
Imran
Hello All
Am trying to read a huge txt file and write to a file back.
pls see the code
int main()
{
char* pData;
//write to some file
delete pData;
}
bool GetData( char* &pData)
{
char pTempFileLocation="C:\\temp\\test.txt";
//read back the data from the temp file
FILE *fp = fopen(pTempFileLocation,"r+t");
//This Code is to find the size of the file
//simplest way
fseek(fp,0L,SEEK_END);
//now file poitner in the last poisition.
//so pos will have the size
long pos = ftell(fp);
//move the file pointer back to begin
//for reading..
fseek(fp,0L,SEEK_SET);*/
//allocate the data with the pos
pData = new char[pos];
//Just Inializing the pData to null
//for safe side initialization
memset(pData,0,pos);
//read the data back and update the passes variable
if(fp)
fread(pData, sizeof( char ),pos,fp);
else
// file not found
return false;
fclose(fp);
return true;
}
The problem is, I am getting the filesize correctly in the pos var. And am
allocatiin the pData correctly.
but while writing back to new file, it writes some more junk characters.Y it
is so.
File size is > 3MB.
THanks in Adv
Am trying to read a huge txt file and write to a file back.
pls see the code
int main()
{
char* pData;
//write to some file
delete pData;
}
bool GetData( char* &pData)
{
char pTempFileLocation="C:\\temp\\test.txt";
//read back the data from the temp file
FILE *fp = fopen(pTempFileLocation,"r+t");
//This Code is to find the size of the file
//simplest way
fseek(fp,0L,SEEK_END);
//now file poitner in the last poisition.
//so pos will have the size
long pos = ftell(fp);
//move the file pointer back to begin
//for reading..
fseek(fp,0L,SEEK_SET);*/
//allocate the data with the pos
pData = new char[pos];
//Just Inializing the pData to null
//for safe side initialization
memset(pData,0,pos);
//read the data back and update the passes variable
if(fp)
fread(pData, sizeof( char ),pos,fp);
else
// file not found
return false;
fclose(fp);
return true;
}
The problem is, I am getting the filesize correctly in the pos var. And am
allocatiin the pData correctly.
but while writing back to new file, it writes some more junk characters.Y it
is so.
File size is > 3MB.
THanks in Adv