Reputation: 73
I am taking share drive path as input and adding extra '\' and assigning to a variable.If I use this variable for opening file with "fopen" error is thrown as "No such file or directory".
But If I give the same path (with extra '\' in the path) in the code itself I am able to access.
command: program \\xyz\abc.txt
(program_name )
1) My code which doesn't work is like this:
In the program I am making the passed in input path as "\\\\xyz\\abc.txt"
by adding extra "\".
then, fopen(var_name,"r"); /* Not working*/
2) Code which works fine:
char arr[100] = "\\\\xyz\\abc.txt"
fopen(arr,"r"); /* works fine */
It seems if the path is known at compile time itself it is working but not at run time.Please suggest what can I do to access path from input not hard coded in program.
Upvotes: 0
Views: 360
Reputation: 552
You need to escape the backslash in filepath during compile time.. i.e. "\xyz\abc.txt" but runtime has only one slash.
Piece of code to read input from user:
char filename[50]; FILE *fp;
printf("Enter the filename \n"); gets(filename);
fp = fopen(filename, "r");
Upvotes: 2