fread() 的行为很奇怪
我在我的 C 程序中遇到一个问题,在使用 fread()
后,文件指针有时会转到文件末尾。
我会尝试更好地解释 - 代码看起来像这样:
dummy = ftell(fp);
fread(&buf, sizeof(unsigned char), 8, fp);
dummy = ftell(fp);
其中 fp
是指向打开文件的文件指针(用“w+”打开它,我将其用作二进制文件,并且我知道我也应该在那里有一个“b”,但我听说添加它并不重要..), dummy
只是一个 unsigned long
变量, buf
是 unsigned char[8]
现在,在调试时,在 fread
之前的 ftell
处, ,虚拟
是262062 在fread
之后的ftell
处,dummy
是262640 即使我只“移动”了 8 个字节..
有人知道这可能是什么原因吗..? 感谢您的帮助 :)
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 :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您不使用
b
打开文件,ftell()
不会返回真相,只是一种仅对有用的“cookie” >fseek()
。有很多不同的实现方式;检查您系统的手册页以了解更多信息。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 tofseek()
. There are a lot of different implementations out there; check the man page for your system to find out more.