ifstream 不读取 EOF 字符
我正在创建一个程序(在 C++ 中),该程序采用 ASCII 文件并从每一行读取一些值,直到到达文件末尾。我正在使用 ifstream 来读取文件,当我使用 ifstream.eof() 方法时,我从未遇到过停止问题。然而,这一次,即使它在我的测试用例中找到了 eof 字符,当我分析其他文件时,它仍然是无限循环,因为它从未找到 eof 字符。这是编码问题还是我的文件问题?
string line = "";
unsigned long pos = 0;
ifstream curfile(input.c_str());
getline(curfile, line);
int linenumber = 0;
cout<<"About to try to read the file"<<endl;
if (!curfile.good())
cout<<"Bad file read"<<endl;
while (!curfile.eof())
{
cout<<"Getting line "<<linenumber<<endl;
linenumber++;
pos = line.find_first_of(' ');
line = line.substr(pos+1, line.size()-1);
pos = line.find_first_of(' ');
current.push_back(atof(line.substr(0, pos).c_str()));
for (int i = 0; i<4; i++)
{
pos = line.find_first_of(' ');
line = line.substr(pos+1, line.size()-1);
}
pos = line.find_first_of(' ');
dx.push_back(atof(line.substr(0, pos).c_str()));
pos = line.find_first_of(' ');
line = line.substr(pos+1, line.size()-1);
pos = line.find_first_of(' ');
dy.push_back(atof(line.substr(0, pos).c_str()));
getline(curfile, line);
}
编辑:当我第一次运行循环时,currentfile.good() 返回 false...我在做什么导致它返回该值?
I am creating a program (In C++) that takes an ASCII file and reads a few values from each line until it reaches the end of the file. I am using ifstream
to read the file, and I have never had problems with it stopping when I use the ifstream.eof()
method. This time, however, even though it found the eof character in my test case, when I analyzed my other files, it is infinite looping because it never finds the eof character. Is this a coding issue, or an issue with my files?
string line = "";
unsigned long pos = 0;
ifstream curfile(input.c_str());
getline(curfile, line);
int linenumber = 0;
cout<<"About to try to read the file"<<endl;
if (!curfile.good())
cout<<"Bad file read"<<endl;
while (!curfile.eof())
{
cout<<"Getting line "<<linenumber<<endl;
linenumber++;
pos = line.find_first_of(' ');
line = line.substr(pos+1, line.size()-1);
pos = line.find_first_of(' ');
current.push_back(atof(line.substr(0, pos).c_str()));
for (int i = 0; i<4; i++)
{
pos = line.find_first_of(' ');
line = line.substr(pos+1, line.size()-1);
}
pos = line.find_first_of(' ');
dx.push_back(atof(line.substr(0, pos).c_str()));
pos = line.find_first_of(' ');
line = line.substr(pos+1, line.size()-1);
pos = line.find_first_of(' ');
dy.push_back(atof(line.substr(0, pos).c_str()));
getline(curfile, line);
}
EDIT: When I first run the loop, currentfile.good() returns false...what am I doing that causes it to return that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,你不应该那样检查。
eof()
在读取失败之后之前不会返回true
。但你可以做得更好(也更容易)!通过隐式转换为
void*
来检查流状态,该转换可在bool
上下文中使用。由于流上的大多数读取操作都会返回对流的引用,因此您可以编写一些非常简洁的代码,如下所示:基本上,它所做的就是说“虽然我可以成功地从
currentfile
中提取一行,执行以下操作”,无论如何,这就是您真正想说的;-);就像我说的,这适用于大多数流操作,因此您可以执行以下操作:
编辑:我重写代码的方式如下:
First thing is first, you shouldn't check like that.
eof()
doesn't returntrue
until after a failed read. But you can do better (and easier)!check the stream state with the implicit conversion to
void*
which can be used in abool
context. Since most of the read operations on streams return a reference to the stream, you can write some very consice code like this:Basically what it is doing is saying "while I could successfully extract a line from
currentfile
, do the following", which is what you really meant to say anyway ;-);Like I said, this applies to most stream operations, so you can do things like this:
EDIT: The way I would rewrite your code is like this:
不要那样做。
EOF
并不是您在阅读时遇到的唯一情况。您可能会遇到很多错误,因此最好的方法是简单地测试流本身:如果您正在读取行,那么最简单的方法是:
Do not do it like that.
EOF
is not the only thing you'll encounter while reading. There's a bunch of errors you might get, and so the best is to simply test the stream itself:If you're reading lines, then, the simplest way is:
对
getline
的第一次调用会触发ifstream
对象上的一个失败位。这就是为什么如果您使用 ios::good() 检查失败位,则永远不会进入读取循环。我会检查line
的值是什么......它可能是空的,这意味着您在读取文件时遇到另一个问题,例如权限问题等。Your first call to
getline
is triggering one of the fail-bits on theifstream
object. That is why if you do a check for a fail-bit usingios::good()
, you never enter your read loop. I would check to see what the value ofline
is ... it's probably empty, meaning you're having another issue reading your file, like maybe permissions problems, etc.问题就在这里:
试试这个:
The problem is here:
Try this: