I have a problem in a C program of mine where after I use fread()
, the file pointer goes to the end of the file sometimes.
I'll try to explain better - the code looks something like:
dummy = ftell(fp);
fread(&buf, sizeof(unsigned char), 8, fp);
dummy = ftell(fp);
where fp
is a file pointer to an opened file (opened it with "w+", I'm using it as a binary file and I know i'm supposed to have a "b" in there too, but I heard its not really important to add it..),
dummy
is just an unsigned long
variable,
and buf
is unsigned char[8]
now, when debugging, at 开发者_开发问答the ftell
before the fread
, dummy
is 262062
at the ftell
after the fread
, dummy
is 262640
even though I only 'moved' 8 bytes..
does anyone have any idea what can be the cause of this..? thanks for your help :)
If you don't use the b
to open the file, ftell()
doesn't return the truth, just a sort of "cookie" that's only useful to fseek()
. There are a lot of different implementations out there; check the man page for your system to find out more.
精彩评论