I have a C program that uses some resources located in the same directory as the executable. When I execute the program from a random working directory (not the directory where the program is located) the resources don’t load, because the relative path I use in the code is not the path where the executable is. How can I solve this nicely?
Share
Pass the path of the directory that contains the resources to the program as an argument and either:
chdir()on Unix andSetCurrentDirectory()on Windows), orIf it is Windows, as the comment on the question suggests, you can obtain the path of the
exeusingGetModuleFileName(), extract the directory from it and avoid having to provide an argument to the program. Then either of two options listed would allow the program to be executed from anywhere and still locate its resources.