C ++ reading txt file?

Hi, I am using ubuntu (Linux) using the g ++ compiler.

I have a very strange situation, yesterday my code worked fine, I did nothing, but today it does not work. 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 don’t understand why, I spent 2 hours of debugging, yesterday, he can read user data, but today I open the same projects, but I can’t read the file. I am 100% sure that the path is correct and the file has content. BUt my result:

Whats happening?

That's all, nothing more. Help me, I'm crazy to look at this stuff !!!!!!!!

+3
source share
1 answer

file.seekg(0, ios::end);will search for the end of the file. You need to go back to the beginning before you start reading, i.e.is.seekg(0, ios::beg);

+6
source

Source: https://habr.com/ru/post/1788711/


All Articles