[紧急] fgets读取文件中的最后一行两次 [英] [URGENT] fgets reading last line in file twice

查看:278
本文介绍了[紧急] fgets读取文件中的最后一行两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我需要以编程方式读取文件,直到文件结束。我的逻辑是

如下:


while(!feof(Fp))

{

fgets(readLine,10000,Fp);

Hi,

I need to read a file programmatically until end of file. My logic is as
follows:

while(!feof(Fp))
{

fgets(readLine,10000,Fp);

推荐答案

DJP写道:
DJP wrote:


我需要以编程方式读取文件,直到文件结束。我的逻辑如下:

while(!feof(Fp))
{

fgets(readLine,10000,Fp);



做点什么。


}

但是有了这个逻辑,根据文件,我正在阅读我的程序
有时读取最后一行两次。谁能告诉我为什么会这样呢?

这很紧急,所以你的快速帮助将不胜感激。
谢谢!!
Hi,

I need to read a file programmatically until end of file. My logic is as
follows:

while(!feof(Fp))
{

fgets(readLine,10000,Fp);

.
.
.
do something
.
.
.
}
However with this logic, depending on the file I am reading my program
sometime reads the last line twice. Can anyone please tell me why this is
happenning?

This is kind of urgent so your speedy help will be greatly appreciated.
Thank you!!



feof()在您尝试读取文件末尾后返回true。

所以这段代码应该更好用:

while (true){

fgets(readLine,10000,Fp);

if(feof(Fp))break;

... < br $>
}


-

问候,

Slava


feof() returns true AFTER you try to read past the end of file.
So this code should work better:
while(true) {
fgets(readLine,10000,Fp);
if( feof(Fp) ) break;
...
}

--
Regards,
Slava


DJP写道:


我需要以编程方式读取文件,直到文件结束。我的逻辑如下:

while(!feof(Fp))
这里你在设置标志之前测试EOF。

该标志由读取动作设置。请参阅下面的常见问题解答。

{

fgets(readLine,10000,Fp);




做点什么。


}

但是有了这个逻辑,取决于我正在阅读我的程序的文件
这很紧急,所以你的快速帮助将不胜感激。
谢谢!!
Hi,

I need to read a file programmatically until end of file. My logic is as
follows:

while(!feof(Fp)) Here you are testing for EOF before the flag is set.
The flag is set by a read action. See the FAQ below.
{

fgets(readLine,10000,Fp);

.
.
.
do something
.
.
.
}
However with this logic, depending on the file I am reading my program
sometime reads the last line twice. Can anyone please tell me why this is
happenning?

This is kind of urgent so your speedy help will be greatly appreciated.
Thank you!!




首先咨询常见问题解答,你会得到更快的结果。

除了C和C ++语言新闻组之外,我删除了所有内容。

-

Thomas Matthews


C ++新闻组欢迎辞:
http://www.slack.net/~shiva/welcome.txt

C ++ Faq : http://www.parashift.com/c++-faq-lite

C常见问题: http://www.eskimo.com/~scs/c-faq/top.html

alt.comp.lang.learn.c-c ++ faq:
< a rel =nofollowhref =http://www.comeaucomputing.com/learn/faq/target =_ blank> http://www.comeaucomputing.com/learn/fa q /

其他网站:
http:/ /www.josuttis.com - C ++ STL图书馆书籍



You''ll get faster results by consulting the FAQ first.
I removed all but the C and C++ language newsgroups.
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book




" DJP" <做*********** @ rediffmail.com>在消息中写道

news:cl ********** @ gist.usc.edu ...

"DJP" <do***********@rediffmail.com> wrote in message
news:cl**********@gist.usc.edu...

我需要以编程方式读取文件,直到文件结束。我的逻辑如下:

while(!feof(Fp))
{

fgets(readLine,10000,Fp);




做点什么



}


你的逻辑非常常见但错误


while(fgets(readLine,10000,Fp),!feof(fp))

{

做点什么

}

然而有了这个逻辑,取决于我正在阅读我的程序的文件
有时读取最后一行两次。有人可以告诉我为什么会这样吗?
Hi,

I need to read a file programmatically until end of file. My logic is as
follows:

while(!feof(Fp))
{

fgets(readLine,10000,Fp);

.
.
.
do something
.
.
.
}
Your logic is very common but wrong

while (fgets(readLine,10000,Fp), !feof(fp))
{
do something
}

However with this logic, depending on the file I am reading my program
sometime reads the last line twice. Can anyone please tell me why this is
happenning?




因为feof的返回只能准确反映

的状态以前的阅读,而不是下一个。


有时似乎世界上每一个新手都错了,所以

你并不孤单。 />

john



Because the return of feof only accurately reflects the status of the
previous read, not the next one.

It sometimes seems that every single newbie in the world gets this wrong, so
you are not alone.

john


这篇关于[紧急] fgets读取文件中的最后一行两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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