c++ - How to get path to current exe file on Linux? -


this question has answer here:

the code below gives current path 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 when run gives me current path exe , name of exe, e.g.:

/home/.../test/main.exe 

i

/home/.../test/ 

i know can parse it, there nicer way that?

dirname function you're looking for.

#include <libgen.h>  ssize_t count = readlink("/proc/self/exe", result, path_max); const char *path; if (count != -1) {     path = dirname(result); } 

Comments

Popular posts from this blog

php - render data via PDO::FETCH_FUNC vs loop -

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

The canvas has been tainted by cross-origin data in chrome only -