Relative path?

Started by
8 comments, last by glPetter3f 16 years, 3 months ago
Hi i am trying to make a simple game that might take a filename as argument so the user can either start the game by doubbleclicking the icon or by dropping a file at the icon. The game has a file called "settings" lying in the same directory as the exe and i open it like this: fstream settings; settings.open("settings",ios_base::binary | ios_base::in); this works fine but for some reason the program can't find the file if I start the program by dropping a file at it. I am doing this under windows and the code is c++. I know i can find the absolute path in argv[0] and use it while opening settings but I don't want to use it because I load a lot ofother files too and i am sure there must be some simple solution like: ChangeWorkingDircetory(Path(argv[0]));
Advertisement
IIRC (and I may not) drag&drop causes the program to be invoked with the dropped file's directory as the program's working directory. To get the directory that the executable is in, use GetModuleFileName.
The function to change the working directory is SetCurrentDirectory(). You can use boost::filesystem to get the directory portion of the file name that GetModuleFileName() returns.
Thanks a lot, but the file settings lies in the same directory as the executable so if the program's working directory is the directory of the file it shouldn't be any difference if i drop it or not, or a i getting it wrong?
Well, the problem there is that, AFAICT, the starting working directory for a program that gets invoked via dropping a file on the application is pretty random, and not necessarily related to the directory of either the dropped file or the executable.
By default in my applications, whether command line or Win32, i always set the working directory to where the executable is. Either using GetModuleName() as specified above for Win32 applications or reading the first string in the command line arguments in the case of a console application.

Note, be careful with paths on the command line that have spaces in.
Hi, thanks for the help. It worked, i did like this:

TCHAR path[1000];
GetModuleFileName(NULL,path,1000);
std::string p=path;
size_t length=p.find_last_of('\\');
p=p.substr(0,length+1);
SetCurrentDirectory(p.c_str());

probably not the niecest way of doing it, does anyone know how to get rid of the 1000 characters limit?

And does anyone know why windows puts you in a random directory if you start a program by dropping a file at it, seems rather stupid to me.
I think Windows starts the program from the "Documents and Settings\username" folder when you drop a file on the executable. But you can never be sure of that - so you have to use GetModuleFileName.

The 1000 characters limit will be o.k. because file names are usually no longer than 256 characters or so. But you have better used path[MAX_PATH+1] (= 260 in my headers). GetModuleFileName offers no way to get the string length beforehand.
Quote:Original post by glPetter3fAnd does anyone know why windows puts you in a random directory if you start a program by dropping a file at it, seems rather stupid to me.
How do you know it's random? Have you tried looking to see what it is using before you change it?

nope, i have no idea of how to do that and i don't think it is random
but someone mentioned it earlier (I now have figured out what AFAICT means :-) ) but i still think it is weird to have a different working directory just because the program is started by dropping a file at it.

This topic is closed to new replies.

Advertisement