Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using relative paths... weird problem .. please help

Status
Not open for further replies.

classic773

Programmer
Jun 14, 2001
57
0
0
US
It is very important for me to be able to use relative paths in my program. The line:
if(pref_file.Open("prefs.txt", CFile::modeRead | CFile::typeText)) Works fine when I'm debugging from within visual c++. However when I double click on the executable, it can't open the file because its trying to open "C:\Program Files\Common Files\System\Mapi\1033\NT\prefs.txt" (this was found from debugging info.) The location of the executable is in a completely different spot. I'm using win2000. Is there a way to set the relative path within the executable. Can somebody please help?
 
Yes, probably this is the default directory of the current process.

Use the API SetCurrentDirectory to change the current directory for the current procesas to the path desired.

Hope this helps, s-)

Blessed is he who in the name of justice and good will, shepards the week through the valley of darkness...
 
Is there a way to find the path that the executable was executed from?
 
char szName[MAX_PATH];
GetModuleFileName(AfxGetInstanceHandle(), szName, MAX_PATH);

This will obtain the full path of your executable

Use GetModuleHandle(0) instead AfxGetInstanceHandle() if you are not in a MFC program

s-)

Blessed is he who in the name of justice and good will, shepards the week through the valley of darkness...
 
Thanks for the tip, however this still gives me the common files.... path. However, if I run the program in the debugger, I essentially get the correct path. If you have any more ideas I'll appreciate them, otherwise I have another solution to implement.
 
Well my alternative method was to search all the hard drive for the executable, get the path of it, and use that path. unfortunately I can't figure out how to do this kind of a search, only a search for a specific directory. Anybody know how to search all drives for a file?
 
to get the current working directory i use _getcwd. It is listed in the index of the help files.

If you are doing directory traversal let me know... I have a program I wrote to rename all files (which could be concidered a virus in the wrong hands as it will rename ANY/EVERY file so be careful where you run it from if you do request it) It is a very dangerous program but I can post the code if you like.

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top