问题描述
我正在创建一个程序(在C ++中),该程序采用ASCII文件并从每一行读取一些值,直到到达文件末尾。我正在使用 ifstream
来读取文件,并且当我使用 ifstream.eof()$ c时,它从未停止过问题$ c>方法。但是,这一次,即使它在我的测试用例中找到了eof字符,但当我分析其他文件时,它却是无限循环的,因为它从未找到eof字符。这是编码问题还是文件问题?
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);
}
编辑:当我第一次运行循环时,currentfile.good()返回false ...我在做什么导致它返回?
When I first run the loop, currentfile.good() returns false...what am I doing that causes it to return that?
推荐答案
第一件事是第一,你不应该像那。 eof()
直到读取失败后才返回 true
。但是您可以做得更好(和更轻松)!
First thing is first, you shouldn't check like that. eof()
doesn't return true
until after a failed read. But you can do better (and easier)!
使用隐式转换为 void * $ c $来检查流状态c>可以在
。由于大多数对流的读取操作都返回对该流的引用,因此您可以编写一些非常简洁的代码,如下所示: bool
上下文中使用
check the stream state with the implicit conversion to void*
which can be used in a bool
context. Since most of the read operations on streams return a reference to the stream, you can write some very consice code like this:
std::string line;
while(std::getline(currentfile, line)) {
// process line
}
基本上,这是在说虽然我可以成功地从 currentfile
中提取一行,但是请执行以下操作,这就是您真正的意思就像我说过的一样,这意味着应该说;-);
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:
int x;
std::string y;
if(std::cin >> x >> y) {
// successfully read an integer and a string from cin!
}
编辑:我将重写代码的方式就像这样:
EDIT: The way I would rewrite your code is like this:
string line;
unsigned long pos = 0;
int linenumber = 0;
ifstream curfile(input.c_str());
std::cout << "About to try to read the file" << std::endl;
while (std::getline(curfile, line)) {
std::cout << "Getting line " << linenumber << std::endl;
linenumber++;
// do the rest of the work with line
}
这篇关于ifstream不读取EOF字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!