post  Get the path of the executable file

santaris (6)   Link to this post
Hello.Is there any function to get the exact path where the executable is even if the executable path changes.I mean that i run the executable in a different path everytime.Thanks in advance.
Zaita (2112)   Link to this post
Yes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    string fullFileName = "";

    // Code taken from: http://www.gamedev.net/community/forums/topic.asp?topic_id=459511
    std::string path = "";
    pid_t pid = getpid();
    char buf[20] = {0};
    sprintf(buf,"%d",pid);
    std::string _link = "/proc/";
    _link.append( buf );
    _link.append( "/exe");
    char proc[512];
    int ch = readlink(_link.c_str(),proc,512);
    if (ch != -1) {
        proc[ch] = 0;
        path = proc;
        std::string::size_type t = path.find_last_of("/");
        path = path.substr(0,t);
    }

    fullFileName = path + string("/");
xcoder (9)   Link to this post
I assume that you're running linux!

Then just type

path = system("echo $PWD");

and your path will hold the current directory where executable is!

Just try it in your terminal type echo $PWD you'll see!
chrisname (1342)   Link to this post
Don't use the "system()" function.
Do it the way Zaita showed you.

Possibly you could pipe the output of something like "ls" with a childprocess and execvp or something, but Zaitas way is already there and probably better.
xcoder (9)   Link to this post
Probably!

This topic is archived - New replies not allowed.