简体   繁体   中英

C++ Reading a txt file?

Hi Iam using ubuntu (Linux), using g++ compiler.

I have a very weird situation, yesterday my code was working fine, I didn't do anything, but today, its not working. Here is my code:

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();
    }

I dont understand why, I spent 2 hours debugging, yesterday, it can read users data, but today, i open the same projects, but it cant read the file. I am 100% sure, the path is correct and the file has contents. BUt my result is:

Whats happening?

Thats all, nothing else. Help me, I am going crazy look at this stuff!!!!!!!!

file.seekg(0, ios::end); will seek to the end of the file. You need to seek back to the beginning before you start reading ie. is.seekg(0, ios::beg);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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