c++ - 到达文件末尾后从文件中读取

标签 c++ visual-studio-2012 eof seekg

我有一个名为 readNextString(ifstream &file , char* &pBuffer) 的函数,它从文件中提取下一个字符串,直到 ',''\到达 n' 时,删除字符串开头和结尾的空格,将其余部分保存在 pBuffer 中,如果一切正常则返回 true - 否则返回 false。一切正常,直到到达文件末尾。设置 eof 标志后,我无法移动 get 指针。 我试过这个:

if(file.eof())
{
   file.clear();
   file.seekg(0 , ios::end)
}

...然后删除字符串末尾的空格。这几乎有所帮助。该函数提取没有空格的字符串,但我得到一个无限循环。

我的实际问题是:如何检查下一个字符是否为 EOF,如果不能,是否有其他方法可以做到这一点?

这是我的实际功能:

bool readNextString(ifstream &file , char* &pBuffer)
{
    if(file.eof()){
        return false;
    }
    for(; file.good() && isWhitespace(file.peek()) && !file.eof() ; file.seekg(1 , ios::cur))
        ;
    if(file.eof()){
        cout << "The file is empty.\n";
        return false;
    }else{
        streamoff startPos = file.tellg();
        cout << "startPos : " << startPos << endl;
        for(;file.good() && file.peek()!='\n' && file.peek()!=',' && file.peek()!= EOF; file.seekg(1 , ios::cur))
            ;
        streamoff A = file.tellg();
        cout << "A : " << A << endl;
        file.seekg(-1 , ios::cur);
        for(;file.good() && isWhitespace(file.peek()) ; file.seekg(-1 , ios::cur))
            ;
        file.seekg(2 , ios::cur);
        streamoff endPos = file.tellg();
        cout << "endPos : " << endPos << endl;
        pBuffer = new char[endPos-startPos];
        if(pBuffer)
        {
            file.seekg(startPos , ios::beg);
            file.get(pBuffer , endPos-startPos , ',' || '\n');
            for(;file.good() && file.peek()!='\n' && file.peek()!=',' && file.peek()!= EOF; file.seekg(1 , ios::cur))
                ;
            file.seekg(2 , ios::cur);
            streamoff temp = file.tellg();
            cout << "temp : " << temp << endl;
            return true;
        }else{
            cout << "Error! Not enough memory to complete the task.\nPlease close some applications and try again.\n";
            return false;
        }
    }
}

这是我称之为的一个地方:

void printCities()
{
    ifstream city ;
    city.open("cities.txt", fstream::in);
    if(city.is_open())
    {
        char *currCity;
        int counter = 1;
        while(readNextString(city , currCity))
        {
            cout << counter++ << ". " << currCity << endl;
            delete[] currCity;
            currCity = NULL;
        }
        if(city.eof())
            cout << "There are no cities added.\n";
        city.close();
    }else
        cout << "Error by opening 'cities.txt'.Make sure that the file exist and try again.\n";
}

希望我说得足够清楚。如果您发现其他错误或可能的错误,我将很高兴听到并从中吸取教训。

最佳答案

How can I check if the next char is EOF?

像这样

if (file.peek() == EOF)
{
    // next char is EOF
    ...
}

关于c++ - 到达文件末尾后从文件中读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16317835/

相关文章:

C++使用cin.get获取字符,遇到EOF不结束while循环

linux - 使用 EOF、EOT、EOL 追加多行制表符分隔文本形式的 bash 脚本

c++eof与空行混淆

c++ - 立体系统 - 使用 OpenCv 获取 3d 位置

C++ 共享库定义和取消定义功能

c++ - error LNK 2019 Unresolved externals Build 在c++编译时出错

visual-studio - 如何用我通过代码手动完成的模板替换 wp8 wmapppmanifest 中的图 block 模板?

c++ - 我可以将AVX/SSE用作AoS布局而不是SoA吗?

c++ 到 c#'s "作为”?

c# - 如何使用 Microsoft Fakes 引用具有自身通用参数的 stub ?