cpp_noob
cpp_noob

Reputation: 253

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!!!!!!!!

Upvotes: 3

Views: 740

Answers (1)

GWW
GWW

Reputation: 44093

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

Upvotes: 6

Related Questions