How to get path to current exe file on Linux?
- by user1519221
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?