Fopen () with relative path

I have a problem using fopen () with a relative path. I wanted to use fopen as follows:

fopen("\\Saurabh\\pqrs.txt"); 

I get filePointer as null.

The situation arose because I was trying to create an installation or deployment project that should read files. The default file paths after the user completes the setup are C: \ Program Files \ Setup .. (where exe is reset). Therefore, I dumped the files in one folder and gave the path (fixed path or hard code) to those files in the program.

If the user selects a different installation path, the program does not work.

Can this be fixed?

+4
source share
2 answers

Two problems:

  • You need to avoid the backslash character. Write \\ .
  • You need to use a relative path. By starting with \\ you mean starting from the root directory.

Putting them together, I think you should write:

 fopen("Saurabh\\pqrs.txt"); 
+6
source

Make sure you double the \ characters. The line you pass in should be "Saurabh\\pqrs.txt" . Note that the beginning of the path with \\ means that it is not relative. (Well, actually this is relative to your current drive, but I doubt what you're looking for.)

+1
source

All Articles