how to write path of external file in an included c++ source -
suppose i'm writing library or set of tools mytool
class mytool
other people can use defined. suppose have directory tree this:
project | - program1 | - main1.cpp ... | - mytool | - mytool.h | - mytool.cpp | - data.txt
in tool1.cpp
use external binary huge file data.dat
:
ifsteam f("data.txt");
the main1.cpp
use mytool, if mytool.(s)o
linked main1.o
program can't find data.dat
, case need change previous line to:
ifstream f("../mytool/data.txt");
but can't know other people put mytool
example can have different directory tree:
project | - program1 | - main1.cpp | - mytool | - tool1.h | - tool2.cpp | - data.dat
in addition (am right?) path depend on program executed.
the solution can imagine pass class contructor mytool
path of data.dat
want keep hidden file user.
you need know absolute path of file, or else path of file relative working directory. 1 approach have configuration script user runs before compiling program. script hardcodes program relevant path, program has path hardwired in manner customized user.
sometimes that's not option because don't want distribute source code, or because wish allow path change @ runtime. can read configuration file @ runtime says file is. layer of abstraction: still need know configuration file is. might, example, ask system user's personal directory is, , find file there @ directory. sort of mix between compile-time , runtime computation of path.
Comments
Post a Comment