0

Edit: solved...using the same ifstream for multiple files is apparently not a good idea.

I just bought a new laptop, and having transfered my C++ project to the new computer, I'm suddenly having an issue with reading text files from Code::Blocks...I'm not sure why.

The old laptop ran a 32 bit OS whereas the new one is 64 bit. I don't know if that would affect anything.

Here's the section of code that is no longer working:

ifstream myfile;
//edit: a different file is read here, in exactly the same manner as below
//when I comment it out, the following section works as it's meant to...
//what am I doing wrong that they won't work successively

myfile.open("version.txt");

if (myfile.is_open()) {
    out("This message displays...");
    //OK so apparently 'eofbit' is thrown here...but the file has content in it?
    while (myfile.good()) {
        out("This message doesn't, when it should...");
        getline(myfile,version_info);
    }
    myfile.close();
}

edit: 'out' is just a custom script to display a message, btw.

AgentAkki
  • 39
  • 5
Matthew
  • 391
  • 1
  • 4
  • 14
  • Is the code compiled for 32 or 64 bits? – Preet Sangha Nov 21 '11 at 04:48
  • @Preet Sangha Well I rebuilt the whole project on the 64bit laptop so I assume that. It works on the 32bit laptop though, and everything else works on the 64bit laptop, just not files... – Matthew Nov 21 '11 at 04:56
  • @keith.layne I know the code is fine, because I've been using it for months on the other laptop. If there's something to debug, I don't know what it is. That's why I've come to you guys ;) – Matthew Nov 21 '11 at 04:58
  • If `myfile.good()` is false, then at least one of `eofbit`, `badbit`, or `failbit` flags is set. `failbit` gets set when an I/O operation fails, so since you don't try that, it must be one of the other flags. – JohnPS Nov 21 '11 at 05:01
  • eofbit triggered. Does that mean the file is empty? It's not though... – Matthew Nov 21 '11 at 05:04
  • Perhaps it is not finding the file, but it creates a file? I don't think it should do that. Could it be opening a file in another directory than you expect? – JohnPS Nov 21 '11 at 05:07
  • I've done a search, all of the version.txt files except for the 'correct' one have not been touched. – Matthew Nov 21 '11 at 05:11
  • Updated OP, possibly found the problem but not sure what's causing it... – Matthew Nov 21 '11 at 05:30
  • 1
    Do you mean you were opening another file with the same variable `myfile` then you opened `version.txt` after that? – JohnPS Nov 21 '11 at 05:39
  • Yes, but there were no problems in the past so I assumed that was OK..? I will try using a different ifstream for the second file...urg...yeah that worked. I don't know why it was fine on my other laptop; thank you! – Matthew Nov 21 '11 at 05:40
  • Not the current problem. But you should note that `while (myfile.good())` is nearly always wrong. It is considered an anti-pattern for reading a file. – Martin York Nov 21 '11 at 05:48
  • @Matthew I wasn't meaning to suggest that your program was wrong, but an interactive debugging session can show you in real time what is actually going on...without modifying your code that you're already pretty confident in. – Keith Layne Nov 21 '11 at 06:01
  • @Loki Astari Hmm, noted. I only use that method as it's what cplusplus.com uses. – Matthew Nov 21 '11 at 06:07
  • @Matthew: Well I can't say I think highly of cplusplus.com lots of incorrect stuff on there (but it seems popular). See http://stackoverflow.com/q/5605125/14065 – Martin York Nov 21 '11 at 06:28
  • My question was what platform is the code compiled for? – Preet Sangha Nov 23 '11 at 08:08

1 Answers1

0

try using myFile.rdstate() to determine what is causing the error see here

smitec
  • 2,999
  • 1
  • 13
  • 12