How to get path to current exe file on Linux?

Posted by user1519221 on Stack Overflow See other posts from Stack Overflow or by user1519221
Published on 2014-05-29T21:23:08Z Indexed on 2014/05/29 21:24 UTC
Read the original article Hit count: 148

Filed under:
|
|

The code below gives current path to exe file on Linux:

#include <iostream>
std::string getExePath()
{
  char result[ PATH_MAX ];
  ssize_t count = readlink( "/proc/self/exe", result, PATH_MAX );
  return std::string( result, (count > 0) ? count : 0 );
}
int main()
{
  std::cout << getExePath() << std::endl;  
  return 0;
}

The problem is that when I run it gives me current path to exe and name of the exe, e.g.:

/home/.../Test/main.exe

I would like to get only

/home/.../Test/

I know that I can parse it, but is there any nicer way to do that?

© Stack Overflow or respective owner

Related posts about c++

Related posts about path