繁体   English   中英

C ++读取txt文件?

[英]C++ Reading a txt file?

嗨,我使用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?

多数民众赞成,没有别的。 帮助我,我疯了看看这东西!!!!!!!!

file.seekg(0, ios::end); 将寻求文件的结尾。 你需要在开始阅读之前回到起点即。 is.seekg(0, ios::beg);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM