Sign in to follow this  

Relative path?

This topic is 3627 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]));

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 3627 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this