This question already has an answer here:
Is there a way in C/C++ to find the location (full path) of the current executed program?
(The problem with argv[0]
is that it does not give the full path.)
This question already has an answer here:
Is there a way in C/C++ to find the location (full path) of the current executed program?
(The problem with argv[0]
is that it does not give the full path.)
Remember that on Unix systems the binary may have been removed since it was started. It's perfectly legal and safe on Unix. Last I checked Windows will not allow you to remove a running binary.
/proc/self/exe will still be readable, but it will not be a working symlink really. It will be... odd.
I would
1) Use the basename() function: http://linux.die.net/man/3/basename
2) chdir() to that directory
3) Use getpwd() to get the current directory
That way you'll get the directory in a neat, full form, instead of ./ or ../bin/.
Maybe you'll want to save and restore the current directory, if that is important for your program.
Use GetModuleFileName() function if you are using Windows.