ifstream 不读取 EOF 字符

发布于 2024-11-17 17:26:21 字数 1229 浏览 2 评论 0原文

我正在创建一个程序(在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

兔姬 2024-11-24 17:26:21

首先,你不应该那样检查。 eof() 在读取失败之后之前不会返回true。但你可以做得更好(也更容易)!

通过隐式转换为 void* 来检查流状态,该转换可在 bool 上下文中使用。由于流上的大多数读取操作都会返回对流的引用,因此您可以编写一些非常简洁的代码,如下所示:

std::string line;
while(std::getline(currentfile, line)) {
    // process line
}

基本上,它所做的就是说“虽然我可以成功地从 currentfile 中提取一行,执行以下操作”,无论如何,这就是您真正想说的;-);

就像我说的,这适用于大多数流操作,因此您可以执行以下操作:

int x;
std::string y;
if(std::cin >> x >> y) {
    // successfully read an integer and a string from cin!
}

编辑:我重写代码的方式如下:

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
}

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)!

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
}

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
}
野稚 2024-11-24 17:26:21

不要那样做。

EOF 并不是您在阅读时遇到的唯一情况。您可能会遇到很多错误,因此最好的方法是简单地测试流本身:

while(currentfile)
{
    // read somehow
}

如果您正在读取行,那么最简单的方法是:

std::string line;
while(std::getline(currentfile, line))
{
    // use line
}

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:

while(currentfile)
{
    // read somehow
}

If you're reading lines, then, the simplest way is:

std::string line;
while(std::getline(currentfile, line))
{
    // use line
}
遗心遗梦遗幸福 2024-11-24 17:26:21

getline 的第一次调用会触发 ifstream 对象上的一个失败位。这就是为什么如果您使用 ios::good() 检查失败位,则永远不会进入读取循环。我会检查 line 的值是什么......它可能是空的,这意味着您在读取文件时遇到另一个问题,例如权限问题等。

Your first call to getline is triggering one of the fail-bits on the ifstream object. That is why if you do a check for a fail-bit using ios::good(), you never enter your read loop. I would check to see what the value of line is ... it's probably empty, meaning you're having another issue reading your file, like maybe permissions problems, etc.

无声静候 2024-11-24 17:26:21

问题就在这里:

if (!curfile.good())
    cout<<"Bad file read"<<endl;   // OK you print bad.
while (!curfile.eof())             // But the loop is still entered.
                                   // Another reason to **NEVER** to use 
                                   // while (file.eof()) // as bad does not mean eof
                                                         // though eof is bad

试试这个:

void readFile(std::istream& str)
{   
    std::string     line;
    while(std::getline(str, line))
    {
        std::stringstream   lineStream(line);
        std::string         ignoreWord;
        int                 number[3];

        lineStream >> ignoreWord   // reads one space seporated word
                   >> number[0]    // reads a number
                   >> ignoreWord >> ignoreWord >> ignoreWords  // reads three words 
                   >> number[1]    // reads a number
                   >> number[2];   // reads a number

        current.push_back(number[0]);
        dx.push_back(number[1]);
        dy.push_back(number[2]);
    }   
}   

The problem is here:

if (!curfile.good())
    cout<<"Bad file read"<<endl;   // OK you print bad.
while (!curfile.eof())             // But the loop is still entered.
                                   // Another reason to **NEVER** to use 
                                   // while (file.eof()) // as bad does not mean eof
                                                         // though eof is bad

Try this:

void readFile(std::istream& str)
{   
    std::string     line;
    while(std::getline(str, line))
    {
        std::stringstream   lineStream(line);
        std::string         ignoreWord;
        int                 number[3];

        lineStream >> ignoreWord   // reads one space seporated word
                   >> number[0]    // reads a number
                   >> ignoreWord >> ignoreWord >> ignoreWords  // reads three words 
                   >> number[1]    // reads a number
                   >> number[2];   // reads a number

        current.push_back(number[0]);
        dx.push_back(number[1]);
        dy.push_back(number[2]);
    }   
}   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文