EOF,当最后一行被读取时永远循环。 c ++ [英] EOF, looping forever when the last line is read. c++

查看:127
本文介绍了EOF,当最后一行被读取时永远循环。 c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  void ReadTheData(ifstream& in_stream,ofstream& out_stream)
{
using namespace std;
int id;
char name [100] =;
双余额;
in_stream>> id>> name>>平衡;
while(name [0] ||!in_stream.eof())//继续循环,直到eof或name不存在。

{
cout<< id<< '\t'<<名称<< '\t'<<平衡< endl
in_stream>> id>> name>>平衡;
}

}

Balance.txt有格式样式:
4248749 Kelly_E 460.02
8898693 Kelly_George_MD 1764.68
4597789 Lambert_Richard 1571.79

当我运行代码时,文件Balance.txt中的最后一行进入无限循环,如何停止无限循环?

解决方案

不知何故这个问题设法得到了100种不同的方式, em>相当其他的副本。更糟糕的是,每次遇到问题时,都会收到一些可笑的回答。



要从流中正确读取数据,具有读取数据的单个条件的循环检查读取成功的尝试是否成功。几乎任何事情都容易失败。特别是,几乎不可能得到一个形式的循环:

  while(!foo.eof())

或:

  while(!foo.bad())

或:

  while(foo.good())

或:

  while(file)

...实际工作正常。每一个都从根本上打破了。获得正确结果的简单方法是将读取和测试结合到单个操作中,例如:

  while(std :: getline(somefile,somestring))

或:

  while(file>> a>> b> c)

至于为什么这些工作和其他工作:文件结尾是不是立即检测到文件指针到达文件结束。相反,它是在您尝试读取某个内容时检测到的,但文件指针已经在文件的末尾。



所以,发生的是,您已读取文件中的所有数据, file.eof()仍为false。您尝试从文件中读取下一个项目(并且失败)。然后您尝试处理数据,即使读取失败。



我推荐工作正常的原因是他们尝试读取数据,然后检查是否成功,并且如果尝试读取失败,则立即退出循环,因此在读取尝试失败后, 不尝试处理数据。



(但密切相关的)点:如果你想读取整个文件,你通常不想基于退出循环专门在检测文件结束。



这些是不同的,如果你遇到一些其他错误,直到到达文件的结尾。在这种情况下,特定地等待文件结束的循环可以(并且通常将)继续无限地运行,重复地尝试已经失败但是未以不写入循环来正确检测的方式失败的操作。 / p>

使用 file.eof()的时间是 循环,并想检测为什么循环退出。如果你已经达到eof,很好 - 你只是读所有的数据。如果你期望循环读取整个文件,但它在到达eof之前退出,那么你可能有一个问题 - 或许是磁盘驱动器的问题,或者只是文件中的坏数据。您通常可以使用 file.eof file.fail file.bad 来排序你遇到什么样的错误(如果有的话)。


void ReadTheData(ifstream& in_stream, ofstream& out_stream)
{
    using namespace std;
    int id;
    char name[100] = "";
    double balance;
    in_stream >> id >> name >> balance;
    while(name[0] || ! in_stream.eof()) // Continue looping until eof or name is not      present.

    {
        cout << id << '\t' << name << '\t' << balance << endl;
        in_stream >> id >> name >> balance;
    }

}

the Balance.txt has format style:
4248749 Kelly_E     460.02
8898693 Kelly_George_MD 1764.68
4597789 Lambert_Richard 1571.79

When I run the code, the last line in file Balance.txt goes into an infinite loop, how do I stop the infinite loop?

解决方案

Somehow this question manages to get asked about 100 different ways, none of which is quite a duplicate of the others. Worse, every time it gets asked, it gets a ridiculous number of bad answers.

To read the data correctly from a stream, you want a loop with a single condition that reads the data and checks whether that attempt at reading succeeded. Almost anything else is prone to failure. In particular, it's nearly impossible to get a loop of the form:

while (!foo.eof())

or:

while (!foo.bad())

or:

while (foo.good())

or:

while (file)

...to actually work correctly. Every one of these is fundamentally broken. The easy way to get correct results is to combine reading and testing into a single operation, such as:

while (std::getline(somefile, somestring))

or:

while (file >> a >> b >> c)

As to why these work and the others don't: end of file is not detected immediately upon the file pointer reaching the end of the file. Rather, it's detected when you attempt to read something, but the file pointer is already at the end of the file.

So, what happens is that when at the point that you've read all the data in the file, file.eof() remains false. You attempt to read the next item from the file (and it fails). Then you attempt to process the data even though the read failed.

The reason the forms I recommend work correctly is that they attempt to read data, then check whether that succeeded, and immediately exit the loop if the attempted read failed, so they do not attempt to process data after a read attempt failed.

As a sort-of separate (but closely related) point: if you're trying to read an entire file, you do not normally want to base exiting the loop specifically on detecting end of file. You want to read as long as you can convert input, and exit the loop as soon as that fails.

These are different when/if you encounter some other error before reaching the end of the file. In such a case, a loop that waits specifically for end of file can (and often will) continue running indefinitely, repeatedly attempting an operation that has already failed, but failed in a way that the loop isn't written to detect correctly.

The time to use file.eof() is after you've exited from the loop, and want to detect why the loop exited. If you've reached eof, great--you just read all the data. If you expected the loop to read the entire file, but it exited before reaching eof, then you probably have a problem -- perhaps a problem with the disk drive, or perhaps just bad data in the file. You can typically use file.eof, file.fail and file.bad to sort out what sort of error you encountered (if any).

这篇关于EOF,当最后一行被读取时永远循环。 c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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