Visual C++ debugger is acting weird...

Started by
13 comments, last by derefed 16 years, 9 months ago
   ifstream file(filename.c_str());      if (file.eof())   {      logfile.Print("EOF!");   }   if (file.bad())   {      logfile.Print("bad!");   }   if (file.fail())   {      logfile.Print("fail!");   }


"filename" is indeed set properly to the input received, according to the debugger. When stepping through this snippet, it steps into the if (file.fail()) block. However, when running the .exe, the logfile never shows a "fail!" line (and the logfile does get other output that I told it to print, so I know it's not a problem with logfile.).
Advertisement
Well now that looks like it just wasn't able to open the file.

You're sure that filename is an absolute path and the file exists? (if it's just a relative path, that could explain the difference right there - it would suggest the current working directory is different when you run it under the debugger, and the file can't be found)

Well, anyway, assuming you do have an absolute path, you need to find the reason the file is failing to be opened. Unfortunately ifstream doesn't give any indication why. I would temporarily switch it to the win32 api CreateFile. Assuming it also fails in that case, GetLastError() should return the error code that indicates why it failed.
if( !file.is_open() ) {   logfile.Print("Not open!");}
Quote:Original post by phil_t
Well now that looks like it just wasn't able to open the file.

You're sure that filename is an absolute path and the file exists? (if it's just a relative path, that could explain the difference right there - it would suggest the current working directory is different when you run it under the debugger, and the file can't be found)


Aye, both a relative and absolute path fail. I did, however, test !file.is_open(), which returns false when debugging, but true when not. Is there some debug working directory path I can change in the VC++ settings somewhere?
Oh wow.

Nevermind, I just answered my own question. In the project settings, under "Debugging", there's a working directory option that is defaultly set to where the project file is (but my file was in the debug build directory). After changing it, the debugger acts as it should. Still don't know why an absolute path failed... but oh well.

Thank you all for your suggestions and help with this, I appreciate it very much.

This topic is closed to new replies.

Advertisement