为什么fread忘记了它的位置? [英] why is fread forgetting where its position?

查看:72
本文介绍了为什么fread忘记了它的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这很奇怪,也许有人可以向我解释。


我使用fread扫描二进制文件并拉出字节。在

中间的一段时间循环,无缘无故我能辨别,fread

突然一遍又一遍地返回同一个字节,好像它是

不再在文件中前进。我使用十六进制编辑器来确定文件中读取的最后一个字节的

地址。 CF是最后一个地址,D0

虽然我的while循环一直在运行但是没有读过。


当我试图使用ftell来尝试调试问题,该程序运行

罚款。嗯,所以我把ftell拿出来,程序再次停止工作

。 ftell没有做任何我能说的事情。这是我的

循环:


而(blah2!= 3220)//我还在制作bug并使用

临时变量,例如blah

{

fread(& newval,BPS,1,WavInPtr); //把新字节放在

newval


blah = oldval ^ newval [0];


ftell( WavInPtr); // fread忘了自己

没有这个

if(blah< 0)

{

oldval = newval [0];

fprintf(WavOutPtr,"%ld \ n",period_cntr);

period_cntr = 0;


}


++ period_cntr;


++ blah2;

}


你可以看到,ftell并没有附加任何东西,它没有做任何我能看到的东西。但如果我把它拿出来,程序就会运行

罚款到某一点然后停止有意义。


我正在编译Dev- c ++ 4.9.9.2


我很乐意将源代码发送给想要玩它的人

自己。它需要一个随附的小wav文件。

I found this very odd and maybe someone can explain it to me.

I was using fread to scan through a binary file and pull bytes out. In
the middle of a while loop, for no reason that i could discern, fread
all the sudden kept returning the same byte over and over as if it were
no longer advancing in the file. I used a hex editor to determine the
address of the last byte read in the file. CF was the last address, D0
was not ever read although my while loop kept running.

when i tried to use ftell to try to debug the problem, the program ran
fine. mmm, so i took the ftell out and the program ceased working
again. the ftell wasn''t doing anything that i could tell. this is my
loop:

while (blah2!=3220) // I''m still working out bugs and using
temporary variables like "blah"
{
fread(&newval,BPS,1,WavInPtr); //put new byte in
newval

blah=oldval^newval[0];

ftell(WavInPtr); // fread forgets about itself
without this
if(blah < 0)
{
oldval = newval[0];
fprintf(WavOutPtr,"%ld \n",period_cntr);
period_cntr=0;

}

++period_cntr;

++blah2;
}

as you can see, ftell isn''t attached to anything, it isn''t doing
anything that i can see. but if i take it out, the program just runs
fine to a certain point and then stops making sense.

I''m compiling on Dev-c++ 4.9.9.2

I''d be glad to send the source code to anyone who wants to play with it
themselves. it requires a small wav file that comes with it.

推荐答案

这些东西被称为返回值。来自图书馆惯例。

看到他们说的话通常是个好主意,比如每次

函数调用。


我怀疑fread正在检测文件结束。很可能,如果它确实

命中文件末尾,或者你在文本中打开了文件。模式。

There are these things called "return values" from library routines.
It''s often a good idea to see what they say, like at each and every
function call.

I suspect fread is detecting end of file. Quite possible, if it did
hit end of file, or if you opened up the file in "text" mode.


Ancient_Hacker写道:
Ancient_Hacker wrote:

这些东西被称为返回值。来自图书馆例程。

看到他们说的话通常是一个好主意,比如在每个

函数调用中。
There are these things called "return values" from library routines.
It''s often a good idea to see what they say, like at each and every
function call.



非常感谢讽刺。实际上,我发布的是调试过程中的最后一步

,这是使程序达到
功能的最低限度。之前我在每个循环后打印了
后的ftell结果。我看到它递增了,但我也看到了程序功能

正确。没有调用ftell(并且没有它),

程序不会超过文件中的某个点。它重复

来自文件中间的相同字节(或者,可能根本不返回

值,没有测试,oops)。 br />

thanks much for the sarcasm. Actually, what i posted was the last step
in the debugging process, the bare minimum to get the program to
function. previously i had been printing the result of ftell after
each loop. I saw it incrementing, but i also saw the program function
properly. without this call to ftell (and without it only), the
program does not procede past a certain point in the file. it repeats
the same byte from the middle of the file (or, possibly, returns no
value at all, didn''t test for that, oops).


我怀疑fread正在检测文件结束。很可能,如果它确实

命中文件末尾,或者你在文本中打开了文件。模式。
I suspect fread is detecting end of file. Quite possible, if it did
hit end of file, or if you opened up the file in "text" mode.



啊,我确实在文本模式下打开了文件。谢谢你指出这一点。

这可能确实会影响一些事情,但是,它并没有解释一个

调用ftell如何允许程序一直读取文件。如果
i击中了一个与eof具有相同位模式的数据点(

它没有,它会达到小数31),它是否会连续返回相同的

字节还是根本没有返回值?或者它会通过

内存向下滚动,读取下一个连续位?


任何对这个怪癖的见解都会很有趣,谢谢。


>这些东西被称为返回值。从库例程。
>There are these things called "return values" from library routines.

>看看他们说的话通常是个好主意,比如每个
函数呼叫。
>It''s often a good idea to see what they say, like at each and every
function call.


非常感谢讽刺。实际上,我发布的是调试过程中的最后一步,这是获得程序功能的最低限度。之前我在每个循环之后都打印了ftell的结果。


thanks much for the sarcasm. Actually, what i posted was the last step
in the debugging process, the bare minimum to get the program to
function. previously i had been printing the result of ftell after
each loop.



你打电话后还打印了fread()的结果吗?

Did you also print the result of fread() after you called it?


> I看到它递增,但我也正确地看到了程序功能。没有调用ftell(并且没有它),
程序不会超过文件中的某个点。它从文件中间重复相同的字节(或者,可能根本不返回任何
值,没有测试,oops)。
>I saw it incrementing, but i also saw the program function
properly. without this call to ftell (and without it only), the
program does not procede past a certain point in the file. it repeats
the same byte from the middle of the file (or, possibly, returns no
value at all, didn''t test for that, oops).



如果fread()返回0或-1,则不要看看缓冲区中的值。

If fread() returns 0 or -1, DON''T EVEN LOOK at the value in the buffer.


>我怀疑fread正在检测文件结束。很可能,如果它确实打到了文件的末尾,或者你在文本中打开了文件。模式。
>I suspect fread is detecting end of file. Quite possible, if it did
hit end of file, or if you opened up the file in "text" mode.


啊,我确实在文本模式下打开了文件。感谢您指出这一点。

这可能会对事情产生影响,但是,它并没有解释
调用ftell如何允许程序一直读取整个文件。如果我已经点击了一个与eof具有相同位模式的数据点(它没有,它会达到十进制31),它是否会连续返回相同的字节或者根本没有回报价值?或者它会通过内存滚动,读取下一个连续的位?


Ah, i did open the file in text mode. Thank you for pointing that out.
This probably does affect things, however, it doesn''t explain how a
call ftell allows the program to read all the way through the file. If
i had hit a data point that had the same bit pattern as an eof (which
it didn''t, it hit decimal 31), would it continuoulsy return the same
byte or have no return value at all? or would it scroll off through
memory, reading the next consecutive bits?



fread()返回一个整数值。它不能返回根本没有返回值。

fread() returns an integer value. It cannot return "no return value at all".


这篇关于为什么fread忘记了它的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆