文件结束问题 [英] end-of-file problem

查看:66
本文介绍了文件结束问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我用这个循环读了一个简单的bmp文件:


while(!feof(fp)){

printf("%x \ n",fgetc(fp));

}

fclose(fp);


一切似乎都是正确的,但在文件的最后,我得到一个奇怪的

" ffffffff"输出。这是怎么来的?实际上,在printf打印一些数据之前,循环必须已经完成了

,或者在这种情况下文件

指针何时会增加?

谢谢,

Markus

Hello,

I read a simple bmp-file with this loop:

while ( !feof(fp) ) {
printf("%x\n", fgetc(fp));
}
fclose(fp);

Everything seems to be correct, but at the end of the file, I get a weird
"ffffffff" output. How does this come? Actually the loop must have been
finished before printf can print some data, or when will the file
pointer increased in this case?
Thanks,
Markus

推荐答案

* Markus Pitha写道,On 20.08.2006 15:01:
* Markus Pitha wrote, On 20.08.2006 15:01:

你好,


我用这个循环读了一个简单的bmp文件:


while(! feof(fp)){

printf("%x \ n",fgetc(fp));

}

fclose( fp);


一切似乎都是正确的,
Hello,

I read a simple bmp-file with this loop:

while ( !feof(fp) ) {
printf("%x\n", fgetc(fp));
}
fclose(fp);

Everything seems to be correct,



不是,请查看常见问题解答。 http://c-faq.com/stdio/feof.html


但是在文件的最后,我得到一个奇怪的

" ffffffff"输出。这是怎么来的?实际上,在printf打印一些数据之前,循环必须已经完成了

,或者在这种情况下文件

指针何时会增加?
but at the end of the file, I get a weird
"ffffffff" output. How does this come? Actually the loop must have been
finished before printf can print some data, or when will the file
pointer increased in this case?



ffffffff恰好与大多数机器上的-1相同。在大多数机器上,EOF也会发生
为-1。所以你打印EOF的价值,从fgetc()返回

。在下一次迭代中,feof()返回true,你的循环

停止。


boa


ffffffff happens to be the same as -1 on most machines. EOF also happens
to be -1 on most machines. So you''re printing the value of EOF, returned
from fgetc(). On the next iteration, feof() returns true and your loop
stops.

boa


>


谢谢,

Markus
>

Thanks,
Markus


你好,


感谢链接,但是我无法处理它,就像在这个例子中一样。我b $ b得到警告,比较指针与NULL,因此一个糟糕的程序

行为就像无限循环。

现在我决定使用以下构造:


fseek(fp,0,SEEK_END);

filesize = ftell(fp);

rewind(fp);


while(ftell(fp)< filesize){

printf("%x \ n",fgetc(fp));

}

fclose(fp);
Hello,

thanks for the link but I couldn''t handle it as in this example. I
got warnings of comparing pointers with NULL and therefore a bad program
behaviour like endless loops.
Now I decided to use the following construct:

fseek(fp, 0, SEEK_END);
filesize = ftell(fp);
rewind(fp);

while (ftell(fp) < filesize) {
printf("%x\n", fgetc(fp));
}
fclose(fp);

ffffffff恰好与大多数机器上的-1相同。

在大多数机器上为-1。所以你打印EOF的价值,从fgetc()返回

。在下一次迭代中,feof()返回true,你的循环

停止。
ffffffff happens to be the same as -1 on most machines.
to be -1 on most machines. So you''re printing the value of EOF, returned
from fgetc(). On the next iteration, feof() returns true and your loop
stops.



谢谢,这听起来似乎合情合理。

Thanks, that sounds plausible to me.


* Markus Pitha写道,2006年8月20日16: 36:
* Markus Pitha wrote, On 20.08.2006 16:36:

你好,


感谢链接,但是我无法处理它,就像在这个例子中一样。我b $ b得到警告,比较指针与NULL,因此一个糟糕的程序

行为就像无限循环。

现在我决定使用以下构造:


fseek(fp,0,SEEK_END);

filesize = ftell(fp);

rewind(fp);


while(ftell(fp)< filesize){

printf("%x \ n",fgetc(fp));

}

fclose(fp);
Hello,

thanks for the link but I couldn''t handle it as in this example. I
got warnings of comparing pointers with NULL and therefore a bad program
behaviour like endless loops.
Now I decided to use the following construct:

fseek(fp, 0, SEEK_END);
filesize = ftell(fp);
rewind(fp);

while (ftell(fp) < filesize) {
printf("%x\n", fgetc(fp));
}
fclose(fp);



请不要。


这样的事情怎么样?

int c;


while((c = fgetc(fp))!= EOF)

printf("%x \ n"; c);

Boa


[snip]


Please don''t.

How about something like this instead?
int c;

while( (c = fgetc(fp)) != EOF)
printf("%x\n"; c);
Boa

[snip]


这篇关于文件结束问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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