Save to Project Path

How would I go about saving to the project's path instead of the current working directory?

Just to rephrase, I want to save my data in the same folder that my executable is in, not in the folder that I start the program in...

A cross-platform solution would be nice.

My other option for now is to use SDL's function SDL_GetPrefPath which lets the OS choose a permanent folder for the files to go. This will work for my current problem, but in the future I want to be able to make updates to my program, so I need to know where that program's executable is... right?
Last edited on
You can pass the path you want to use as a command-line option, then call chdir( 2 ) to move to the directory of your choice. You can call getcwd( 3 ) to insure you are in the right place.
I'm not aware of any portable method to get the path to the executable program. One thing you could try is to examine argv[0] (where argv is the 2nd argument to main). It might contain the full path. If it doesn't, then you could go through the trouble of examining the PATH environment variable to see if argv[0] appears in any of the paths specified. This will work in many systems, but note that it's very easy to trick. Although argv[0] may be the name of the program when it's started from the shell, a program could exec() your program and pass anything it wanted in argv[0].
Topic archived. No new replies allowed.