How can I open a file without typing in the full path?
-
I open a file for reading by typing: ofstream datain("c:\\my_program\\data\\my_file.txt", ios::in); The problem is, when I give the program to some people, they have the data on the c: drive while others have data on the d: drive or something different. Is there any way of opening this file without specifying the drive? I tried typing: "..\\my_program\\data\\my_file.txt" as well as "../my_program/data/my_file.txt" but nothing has worked yet!! Please, any response any one can give me will be greatly appreciated. Sincerely, Erich J. Ruth (an overworked graduate student)
-
I open a file for reading by typing: ofstream datain("c:\\my_program\\data\\my_file.txt", ios::in); The problem is, when I give the program to some people, they have the data on the c: drive while others have data on the d: drive or something different. Is there any way of opening this file without specifying the drive? I tried typing: "..\\my_program\\data\\my_file.txt" as well as "../my_program/data/my_file.txt" but nothing has worked yet!! Please, any response any one can give me will be greatly appreciated. Sincerely, Erich J. Ruth (an overworked graduate student)
You may retrieve the current dir, or better the executable path, and then append the name of the file. If you want only the current drive
GetCurrentDirectory()
is right, just take the first char. If you want the current executable path, useGetModuleFileName(NULL,...)
and cut off the text after the last backslash. Then you may append your file's relative path. Paolo.