1

I am trying to hard code into C++ program to look for config.ini in the same directory as the executable, without knowing the complete path to the file. I am trying to find a way to make a local reference to the executable.

Basically load ("./config.ini") without doing

("C:\foo\bar\config.ini")

jscott
  • 397
  • 2
  • 5
  • 18

4 Answers4

2

There isn't really any guaranteed portable way of doing this, but I like to use this code because it works in the vast majority of cases (unless symlinks or other magic is involved):

boost::filesystem::current_path(boost::filesystem::path(argv[0]).remove_filename());

If you are willing to use platform specific code look at GetModuleFileName on Windows and a mix of getpid, reading from /proc and readlink on Linux.

xDD
  • 3,443
  • 1
  • 15
  • 12
1

You want GetModuleFilename() on Windows (pass NULL to get filename of current executable). Otherwise, call boost::filesystem::initial_path() early in the program (see Boost docs in link for the reason to do this early). That should cover most of the situations.

Edit

Brain malfunction. We always start our programs from the executable's directory, so the boost::initial_path() thing works, but it won't work so well if you start the program from another direcory. Sorry for the confusion on that. On Windows, though, I'd get the path from GetModuleFilename and use boost::path to manipulate the result.

gregg
  • 1,027
  • 5
  • 6
  • `GetModuleFilename` has the correct behavior, but `initial_path()` is not useful here since it is trivial to start a process with a CWD different than the directory containing the executable. – ildjarn May 31 '11 at 20:26
  • Must have been editing to that effect when you were writing this. Thanks. – gregg May 31 '11 at 20:30
0

For windows, this will get the directory containing the excutable as a c++ string:

#include <windows.h>
#include <string>
#include <iostream>
using namespace std;;

string ExePath() {
    char buffer[MAX_PATH];
    GetModuleFileName( NULL, buffer, MAX_PATH );
    string::size_type pos = string( buffer ).find_last_of( "\\/" );
    return string( buffer ).substr( 0, pos);
}

You can then just tag the name of your config file on the end.

-1

For Windows:

#include <direct.h>

char cur_path[FILENAME_MAX];

if (!_getcwd(cur_path, sizeof(cur_path)))
{
    // deal with error
}

cur_path[sizeof(cur_path) - 1] = '/0'; // adding \0 at the end of the string

printf("Current dir: %s", cur_path);

A platform-agnostic solution was discussed here:

How do I get the directory that a program is running from?

Community
  • 1
  • 1
karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • 1
    He's not asking how to get the current working directory, but how to get the directory of the executable. – xDD May 31 '11 at 20:20
  • The OP is going to hardcode this checking into his application. Whenever the application is executed, _getcwd() will return the current directory, which is where config.ini is located. – karlphillip May 31 '11 at 20:47