How to open a file in $PATH?

P

PengYu.UT

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?

Thanks,
Peng
 
M

Mike Wahler

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
 
P

PengYu.UT

Mike said:
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.

The OS is linux.

Suppose I use ":" to separate directories, it seems the following like
is enough to split the string into multiple directory names.
http://groups.google.com/group/comp...0?q=split+string&rnum=4&#doc_faf66cfee6b6c320
Is this way good enough?

Once I have the directory names, should I first check if the file is
exist by "stat"
http://groups.google.com/group/comp...stance+of+a+file&rnum=1&#doc_1f43bcd8cbe72933
Is there any other way?

Thanks,
Peng
 
C

Clem.Dickey

getenv() is in C++, since it was in C. It's in the <cstdlib> header.
Aside from that, there's no support for environment variables in C++.
 
C

Clem.Dickey

A split function should work.

The "stat" is part of POSIX, and not C++. Instead, just open a file and
use the istream::good() function to determine whether the open
succeeded.
 
R

Rolf Magnus

Mike said:
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).

getenv is nont a compiler-specific extension. It's part of the C++ standard
library.
 
M

Mike Wahler

Rolf Magnus said:
getenv is nont a compiler-specific extension. It's part of the C++
standard
library.

Oops, my mistake. Of course you're right.

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top