How can the currently running C program know which directory it is in?
Let's say I have a C command line program currently running and I want to read a file or execute another binary in the same directory - how can I know which directory is it?
Please note that I am not looking for the current working directory. A user can invoke my original program in any of the following ways (and possibly others that I am not aware of).
- ../../program
- / home / matte / program
- PATH = $ PATH: / home / matte program
Ideally I am looking for something that will work on Unix system and windows via MinGW.
+2
source to share
2 answers
http://c-faq.com/osdep/exepath.html
As per the C frequently asked question, it cannot be reliably executed
Finding the current path of the executable without / proc / self / exe
+2
source to share