<Pe*******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
The following code open the file "example.txt" in the current
directory.
int main () {
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}
But I'm wondering how to code to support the search path such as
$AWK_PATH?
That is to say if I want to open some file, if it is not
under the current directory, it will search all the directories in
$AWK_PATH until one is found. Is there any standard C++ package to
support this? Or is there any sample code?
Not with only standard C++, since it has no notion of
'search path'. However it could be done with a bit of
help from a compiler and/or operating system that
features a 'search path'. Some operating systems
provide this with an 'environment variable'. Programmatic
access to such a variable is typically done with some non-
standard compiler-specific extension function such as 'getenv()'
(check your documentation). Once you've retrieved the string(s)
from such a 'path variable', you can use standard string-handling
functions to parse them, and use them as arguments to file-opening
functions.
Since this is a platform-dependent operation, I can't offer example
code for the 'search path' retrieval. However, once you've figured
how to do it on your system(s), if you still have trouble e.g.
isolating each path as a string, post the (standard) C++ code
you've tried, and we'll offer further assistance.
-Mike