用法 [英] feof usage

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

问题描述

我教授C的教授说,使用feof测试EOF是否达到
,同时在文件上使用fgets这样的函数是可以的。但是我已经告诉其他人说b b $ b不行。这是怎么回事?是不是没有b $ b好​​吗?


nethlek

解决方案

< blockquote> On Fri,2003年9月19日19:11:20 +0000,Mantorok Redgormor写道:


我教C的教授说用feof来测试EOF是否是
使用像文件上的fgets这样的功能是可以的。但是我已经被别人告知过它不行。这是怎么回事?它好不好?




问题是feof()只会在你试图阅读之后告诉你

过了文件的结尾。如果您理解了这一点,并且相应的代码,那么它可以使用它,AFAIK。


也就是说,如果一个文件有n个字符,你可以用fgets读取所有这些内容

和feof()仍将返回零(false)。但是如果你试着读n + 1,

那么fgets仍然会成功,但是feof()会返回非零(true)。


HTH


Mac

-


Mantorok Redgormor写道:

我教C的教授说,使用feof来测试是否在使用文件上的fgets等函数时达到EOF是可以的。但是我已经被别人告知过它不行。这是怎么回事?它好不好?



没关系 - 但是feof()只返回*之后的非零('true'')值* br />
尝试'读''失败。


HTH,

--ag

- -

Artie Gold - 德克萨斯州奥斯汀


2003年9月19日19:11:20 -0700, ne ***** @ tokyo.com (Mantorok Redgormor)

在comp.lang.c中写道:

我教C的教授说,使用feof来测试是否在使用文件上的fgets等函数时达到EOF是可以的。但是我已经被别人告知过它不行。这是怎么回事?它好不好?




这在常见问题解答中有介绍,特别是在
http://www.eskimo.com/~scs/C-faq/q12.2.html


一般来说,以这种方式使用feof()并不是一个好主意。如果您在输入操作之后并且在尝试使用输入数据之前测试EOF,那么这不是很糟糕。但它仍然可能导致问题,因为错误

不会导致feof()返回非零值。


有各种各样的东西这可能会导致文件错误

操作。硬盘现在非常可靠,但它们仍然是磨损部件。或者用户可以删除包含文件的媒体,例如软盘或CD,例如
。或者网络连接可能会断开。


所有可以从文件中读取的函数都返回一个值,表示

是否成功或失败。 fgets()返回指针,如果成功,则返回
传递它的指针,如果失败则返回空指针。所以在循环中使用

fgets():


while(NULL!= fgets(buffer,sizeof buffer,file_ptr))

{

/ * ... * /

}


输入功能指示失败后,您可以使用feof()和

ferror()来确定你是否成功读取整个文件或

是否发生了错误。


-

Jack Klein

主页: http ://JK-Technology.Com

常见问题解答

comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html

comp.lang.c ++ http://www.parashift。 com / c ++ - faq-lite /

alt.comp.lang.learn.c-c ++ ftp://snurse-l.org/pub/acllc-c++/faq


My professor that teaches C says that using feof to test if EOF is
reached while using a function like fgets on a file is okay. but i''ve
been told elsewhere that it is not okay. what is the case here? is it
okay or not?


nethlek

解决方案

On Fri, 19 Sep 2003 19:11:20 +0000, Mantorok Redgormor wrote:


My professor that teaches C says that using feof to test if EOF is
reached while using a function like fgets on a file is okay. but i''ve
been told elsewhere that it is not okay. what is the case here? is it
okay or not?



The issue is that feof() only tells you AFTER you have attempted to read
past the end of the file. If you understand that, and code accordingly, it
is OK to use it, AFAIK.

That is, if a file has n characters, you can read all of them with fgets
and feof() will still return zero (false). But if you try to read n+1,
then fgets will still succeed, but feof() will return non-zero (true).

HTH

Mac
--


Mantorok Redgormor wrote:

My professor that teaches C says that using feof to test if EOF is
reached while using a function like fgets on a file is okay. but i''ve
been told elsewhere that it is not okay. what is the case here? is it
okay or not?


It''s OK -- but feof() only returns a non-zero (`true'') value *after*
an attempted `read'' has failed.

HTH,
--ag
--
Artie Gold -- Austin, Texas


On 19 Sep 2003 19:11:20 -0700, ne*****@tokyo.com (Mantorok Redgormor)
wrote in comp.lang.c:

My professor that teaches C says that using feof to test if EOF is
reached while using a function like fgets on a file is okay. but i''ve
been told elsewhere that it is not okay. what is the case here? is it
okay or not?



This is covered in the FAQ, specifically at
http://www.eskimo.com/~scs/C-faq/q12.2.html

Generally it is not a good idea to use feof() this way. It is not so
bad if you test for EOF after an input operation and before trying to
use the input data. But it can still cause problems, because an error
does not cause feof() to return a non-zero value.

There are all kinds of things that can cause an error in a file
operation. Hard drives are pretty reliable these days, but they are
still wear parts. Or a user could remove the media containing a file,
such as a floppy disk or CD. Or the network connection could go down.

All functions that can read from a file return a value that indicates
whether they succeeded or failed. fgets() returns the pointer you
passed it if it succeeded, or a null pointer if it failed. So to use
fgets() in a loop:

while (NULL != fgets(buffer, sizeof buffer, file_ptr))
{
/* ... */
}

After your input function indicates a failure, you can use feof() and
ferror() to determine whether you successfully read the entire file or
whether an error occurred.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq


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

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