C, open files relative to executable's path?

2.8k views Asked by At

I want to fix an old C program (got the source) that uses relative paths to load data files, expecting the executable is called from the directory where it is placed, such as LoadEx("./dataFile", dataFile); Of course calling the program from another directory fails since the dataFile cannot be loaded.

How can I modify the C program to load dataFiles relative to the executable's directory?

EDIT: My original question turned inapplicable and so is a duplicate to at least: - How do I find the location of the executable in C? - How to open a file with it's relative path in Linux? - Finding current executable's path without /proc/self/exe

1

There are 1 answers

5
Duck On BEST ANSWER

Why don't you want readlink("/proc/self/exe")? This is the way one finds the executable's path on linux.

The only other way is pull the PATH from the environment with getenv() and walk the PATH directories looking for a match for argv[0]. The first option is much easier.

Or write a script to initiate the program after first doing a which on the executable and passing the result in as a command line argument or some such hack.