3

嗨,我正在使用 ubuntu (Linux),使用 g++ 编译器。

我有一个非常奇怪的情况,昨天我的代码工作正常,我什么也没做,但今天,它不工作。这是我的代码:

ifstream file;
file.open("users.txt", ios::in);

if(file.is_open()){
    int counter = 0;
    string readLine;
    file.seekg(0, ios::end);
    if (file.tellg() == 0)
        file.close();
    else {
        while(!file.eof()){
            getline(file,readLine);
            cout << "whats happening?" << readLine << endl;
            // I was suppose to do process here, but i comment it for debug purposes
        }
        openFile.close();
    }

我不明白为什么,我花了2个小时调试,昨天它可以读取用户数据,但是今天我打开相同的项目,但它无法读取文件。我 100% 确定,路径正确且文件有内容。但是我的结果是:

Whats happening?

仅此而已,没有别的。帮帮我,我要疯了看看这些东西!!!!!!!!!!

4

1 回答 1

6

file.seekg(0, ios::end);将寻找到文件的末尾。在开始阅读之前,您需要回到开头,即。is.seekg(0, ios::beg);

于 2011-01-29T18:52:34.417 回答