pFile = fopen

I working with trying to access a file with pFile. I save my file I want my program to open as myfile.txt format on my desktop. In Visual Studio I set my code up as:

pFile = fopen ("myfile.txt", "r");

I get back NULL and fails to open. Is there a place to store the file I'm trying to open? I can't have it on my desktop? I'm learning, so any advice would be great, thanks,

jer
Just putting the file anywhere will not work because your program will have no way to find it. If you don't specify a directory, it will look in the "current" directory.

If you are running the exe by double-clicking it, the current directory is likely the same directory as the exe.

If you are running from the IDE, the current directory is likely the directory that your project file is in.

Alternatively, you can put a fully qualified path name in your fopen:

 
fopen("C:/path/to/the/file.txt","r");
for example this is the path where I put the file.txt. I saved it in the same folder as the cpp file is in within visual studio. still not working. I am obvoisly missing something.

pFile = fopen("C:\\Users\Jer\Documents\Visual Studio 2010\Projects\filetemp\filetemp\file.txt","r");

Thanks again for your help,

jer
\filetemp\filetemp\

Is this how the directory is set up?
\ is an escape character. You'd have to replace all those \s with \\.

Or you can not use \ at all and use the easier (and more portable) / as I did in my example.
Topic archived. No new replies allowed.