I have a class method (implemented in a shared object in UNIX environment) which needs to access a text data file in runtime (using ifstream). Currently the method assumes that the data file is available for opening without any relative path, i.e something like
ifstream dataFile("data.txt");
The shared object is loaded from python code, and in order for it to be available for loading, it is being copied to the \usr\lib\
folder as a post-build step of the makefile. My question is how to make the text data file available for the shared object. I have considered the following possibilities:
- Use some relative path, but that method is not totally fool-proof (the project is hosted on various instances and I cannot be su开发者_如何转开发re the directory tree will stay the same (e.g) a month from now).
- copy the data file as well to
\usr\lib
, but I feel this is a wrong attitude.
Any suggestions are welcomed.
The proper way to go about this is to make the location of the text file a configurable value that will be set when your project is installed. Using a configuration file in /etc/ is a common way to store that value.
That way you can put the text file in e.g. /usr/share/ with all the machine-independent files (that data file is machine-independent, right?) and your code would "know" where to find it.
Note that if the data file is going to be modified as part of your code's operation, then it should probably be placed somewhere under /var (/var/lib or perhaps /var/cache) according to the Filesystem Hierarchy Standard (FHS) and most other Unix filesystem standards.
If the data file could be considered a configuration file, as you mentioned in one of your comments, you could just hard-code its path to somewhere under /etc/ (e.g. /etc/MyProject/data.cfg) and go on.
I can think of two solutions :
- When you load your shared object, you somehow give it the path to your file.
- Instead of copying the file to /usr/lib you could create a symbolic link do it in /usr/lib but that is not the best thing to do imho.
The first solution is the best one for me.
精彩评论