I have a C++ project with a folder structure something like so:
root/trunk
root/trunk/src/...
root/trunk/include/...
root/trunk/utils/... <-- this has the `main` programs which are compiled
root/trunk/bin/... compiled binaries
root/data/... data used in the programs
In my source files, I sometimes refer to files included in the data subfolders. I want the paths I use to be relative paths (because I am sharing this project with someone else).
How can I ensure that the data files can always be found? Currently I use paths such as the following:
std::string my_data("../../../data/path/to/file.txt");
However, I find it difficult using this to work out where the relative path leads to, and can be messy when source files are moved. Is the a better way of accomplishing the same thing?
A common method of organizing data files is to establish a “data root” folder at program startup, and then code your paths relative to that. The data root can be automatically determined by searching (the current directory and perhaps its parent, grandparent, etc, up to the real root.) for a specially named file or directory (eg “.foo”), and overridden by an environment variable or command-line switch (eg FOO_DATA_DIR=”/home/fred/bar”).
For conveniance the program could change its current working directory to the data root, or alternatively you can resolve your data root relative paths programmatically in userland.