I'm wondering how I can get the directory of my program on Linux. For instance, if I have my program located under /home/myproject/
and I get the directory, it should be /home/myproject/
regardless of what directory I'm calling the program from. I need this functionality because I need to be able to access a configuration file located under the same folder as my program, regardless of where the program's folder is located.
I've tried using getcwd()
, but here's what it does:
If I'm currently in the same folder as the program is, it will work. However, if I was in /root
and tried executing the program which is located under /home/myproject
, it would give me /root
.
If I just do something like...
std::ifstream is("anotherfile");
It will work as long as I'm in the same directory, but it does the same as above when I'm not.