By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 898 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

filesize error

P: n/a


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
GetData( pData);

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.

am using MSVC as the compiler

THanks in Adv

Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Imran wrote:
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
GetData( pData);

delete pData;
You mean

delete[] pData;

, don't you?

}
bool GetData( char* &pData)
{

char pTempFileLocation="C:\\temp\\test.txt";
That shouldn't compile. Did you mean to write

const char* const pTempFileLocation = "C:\\temp\\test.txt";

?


//read back the data from the temp file
FILE *fp = fopen(pTempFileLocation,"r+t");
Opening the file as text is what screws it up. Open it as binary.


//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.

am using MSVC as the compiler


See my comment inline with your post.

HTH

V
Jul 22 '05 #2

P: n/a

//read the data back and update the passes variable
if(fp)
fread(pData, sizeof( char ),pos,fp);
am using MSVC as the compiler


try: fread(pData, pos, 1, fp);

I think I got some problem with this as well, but I can't remember
anymore...

If not, let us see the writing code...
-Gernot
Jul 22 '05 #3

P: n/a

"Imran" <im******@in.bosch.com> wrote in message
news:cf**********@ns1.fe.internet.bosch.com...


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
GetData( pData);

delete pData;
delete [] pDate;
// you allocate an array, so you must delete an array

}


I'm not that familiar with the file functions, so I won't analyse the read
function code. But, you've only *given* a read function! You say that you
have a problem writing back to a file, but you don't show any write
function. Maybe the problem's there?

-Howard
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.