views:

45

answers:

2

I have the following code :

FILE *fp = fopen( srcFile.filename.c_str(), "rt" );

srcFile happens to be the solution's main.cpp file, and thus is opened in the solution. fopen returns NULL most of the time (but not when I step into it, which is weird).

However, when I close main.cpp in Visual Studio, the code works. Even when the file is opened in Notepad++.

Any workaround ?

PS I tagged it as C but the file is compiled as c++, IDK if it changes anything.

+1  A: 

I think that this may have to do with the working directory (folder) being different when running within VS. If this is the case then using an absolute path to the file ("C:\folder\other-folder\file.txt") instead of a relative path ("file.txt") should make it work.

nategoose
the path already is absolute.
Calvin1602
A: 

Let C print the errormessage:

FILE *fp = fopen( srcFile.filename.c_str(), "rt" );
if( !fp ) 
{
  perror( srcFile.filename.c_str() );
  exit( 1 );
}
"Permission denied". This seems logical, since my explanation was that Visual Studio locks the file.
Calvin1602