• Advertisement
Sign in to follow this  

Program directory C++

This topic is 2773 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

My program needs to load a file which is kept in the same directory as the executable, so what i need is to find the directory in which the program exists on the system.

the first command line argument isn't sufficient for this since in the case that the program's directory exists as an enviromnet variable and is executed from elsewhere like:

'>> plc -args'

all you get is 'plc' which isn't too useful...

any ideas?

Share this post


Link to post
Share on other sites
Advertisement
Assuming Win32, you can use GetModuleFileName(NULL, ...) to get the path to the EXE file, then strip everything past the last slash to get the path the EXE is in.

Share this post


Link to post
Share on other sites
I'm not aware of a truly cross-platform solution for this one because the concept of a "program directory" doesn't really necessarily apply to all platforms.
For instance, on Linux / UNIX you can always get the current working directory just like on Windows, but I don't think those platforms provide a means of getting the directory in which the executable used to start the program resides.

Share this post


Link to post
Share on other sites
there is a function getcwd (i never used that before. Just google'd and found)

Share this post


Link to post
Share on other sites
The boost::filesystem library has a function for this that works on most major platforms. It's not EXACTLY that actually--it gives you the current working directory upon application startup, which isn't necessarily the same directory as the executable.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement