file size error - urgent

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
 
E

Emmanuel Delahaye

Imran wrote on 10/08/04 :
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;

Not a C word.

you want

}
bool GetData( char* &pData)

I confirm...

Please learn to make the difference between C and C++. Ther are
different languages.

BTW, The 'urgent' word may have an unexpected side effect...
 
C

Christopher Benson-Manica

Imran said:
bool GetData( char* &pData)

You're either using a C++ compiler or you didn't post your real code.
Either way, you have a problem WRT this newsgroup.
{
char pTempFileLocation="C:\\temp\\test.txt";

Clearly not your real code. Do post it, pray.
//read back the data from the temp file

And you seem to be using a C++ compiler...
//allocate the data with the pos
pData = new char[pos];

Yep. Get thee to comp.lang.c++!
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.

Perhaps you'd like to post the code where you do that? I bet you're
missing a '\0' on the end of the string you write to the file.
 
S

SM Ryan

# //read back the data from the temp file
# FILE *fp = fopen(pTempFileLocation,"r+t");

Why not do something like
if (!fp) perrror(pTempFileLocation);
so that if the fopen is failing, you can get some error message.
 
K

Kenneth Brody

Imran wrote:
[...]
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);
[...]

Well, ignoring the fact that you appear to have C++ code, and not C, my
understanding is that on a text file (as noted by your "r+t" on fopen),
the number of bytes gotten with fread() is not necessarily the same as
the poisition of EOF. (In fact, under DOS/Windows, you are virtually
guaranteed that fread will get less bytes than your fseek/ftell combo
tells you are there.) This is because end-of-line will be returned as
a single '\n' character even though it may be stored on disk as more
than a single byte.

For example, a text file on Windows containing a single line of the
word "foobar" will contain 8 or 9 bytes according to fstat or the
fseek/ftell combo.

It will either be:

66 6f 6f 62 61 72 0d 0a

or

66 6f 6f 62 61 72 0d 0a 1a

In either case, fread will only get 7 characters:

66 6f 6f 62 61 72 0a
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top