3

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

1
  • 3
    yes, it is. Why do you ask? All the code I listed above are all written by myself, of course its a very simple and standard codes though Commented Jan 29, 2011 at 18:52

1 Answer 1

6

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

Sign up to request clarification or add additional context in comments.

3 Comments

I just remember, "file.seekg(0, ios::end);" is the last code I added, to test if the file has no content. You saved my life!!!
@cpp_noob -- next time you find yourself saying "I didn't do anything, it just stopped working" just remember this "I just remember" moment! If it used to work and now it doesn't, something has changed, and all too often it's something you did yourself.
Haha.. thanks AAT, learned a precious lesson today. I will use subversion in future, to prevent this kind of stupid mistake happen again =)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.