C ++ eof()问题 - 从不返回true? [英] C++ eof() problem - never returns true?

查看:155
本文介绍了C ++ eof()问题 - 从不返回true?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想读这个文件。一切看起来像它应该工作,但在运行时程序超时并停止工作,我不得不关闭它。到底是怎么回事?我怀疑oef()测试永远不会返回true,它一直在文件中寻找更多。我没有在文本文件中拖动空行。我试过调试这个疯了。我找不到任何错误,但仍然拒绝工作。

So I'm trying to read this file. Everything looks like it should work, but during runtime the program times out and stops working, and I have to close it. What is going on? I suspect that the oef() test is never returning true and it keeps looking for more in the file. I have no dragging empty lines in the text file. I've tried debugging this like crazy. I can't find anything wrong but it still refuses to work.

Pet** petArray;

ifstream textFile2;
textFile2.open("pets.txt");

int i = 0;
string temp;
int tmpNum = 0;

if (textFile2.is_open())
{
    while (!textFile2.eof())
    {

        getline(textFile2, temp);

        petArray = new Pet*[arraySize];

        if (temp == "Dogs" || temp == "Cats" || temp == "Iguanas" || temp == "Pigs")
        {
            if (temp == "Dogs") tmpNum = 0;
            if (temp == "Cats") tmpNum = 1;
            if (temp == "Iguanas") tmpNum = 2;
            if (temp == "Pigs") tmpNum = 3;
            temp == "";
        }
        else
        {
            if (tmpNum == 0)
            {
                petArray[i] = new Dog(temp);
                cout << "Dog " << temp << " added" << endl;
            }
            if (tmpNum == 1)
            {
                petArray[i] = new Cat(temp);
                cout << "Cat " << temp << " added" << endl;
            }
            if (tmpNum == 2)
            {
                petArray[i] = new Iguana(temp);
                cout << "Iguana " << temp << " added" << endl;
            }
            if (tmpNum == 3)
            {
                petArray[i] = new Pig(temp);
                cout << "Pig " << temp << " added" << endl;
            }
            arraySize++;
        }

        i++;
    }
}

这里是文本文件的格式:

Here is the format of the text file:

Dogs
d1
d2
Cats
c1
c2
Iguanas
i1
i2
Pigs
p1
p2

有任何建议吗?

推荐答案

eof 之后尝试读取某些内容,操作失败。因此,在 getline 后面输入。

eof returns true after you tried to read something and the operation failed. So put it after getline.

编辑:尝试此代码:

vector<Pet*> petArray;
ifstream textFile2("pets.txt");

string temp;
int tmpNum = 0;

while (getline(textFile2, temp))
{
    if (temp == "Dogs") tmpNum = 0;
    else if (temp == "Cats") tmpNum = 1;
    else if (temp == "Iguanas") tmpNum = 2;
    else if (temp == "Pigs") tmpNum = 3;
    else
    {
        if (tmpNum == 0)
        {
            petArray.push_back(new Dog(temp));
            cout << "Dog " << temp << " added" << endl;
        }
        if (tmpNum == 1)
        {
            petArray.push_back(new Cat(temp));
            cout << "Cat " << temp << " added" << endl;
        }
        if (tmpNum == 2)
        {
            petArray.push_back(new Iguana(temp));
            cout << "Iguana " << temp << " added" << endl;
        }
        if (tmpNum == 3)
        {
            petArray.push_back(new Pig(temp));
            cout << "Pig " << temp << " added" << endl;
        }
    }
}

这篇关于C ++ eof()问题 - 从不返回true?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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