How to open a file in $PATH?

Discussion in 'C++' started by PengYu.UT@gmail.com, Jan 22, 2006.

  1. Guest

    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
     
    , Jan 22, 2006
    #1
    1. Advertising

  2. Mike Wahler Guest

    <> wrote in message
    news:...
    > 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
     
    Mike Wahler, Jan 22, 2006
    #2
    1. Advertising

  3. Guest

    Mike Wahler wrote:
    > <> wrote in message
    > news:...
    > > 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.


    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
     
    , Jan 22, 2006
    #3
  4. Guest

    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++.
     
    , Jan 22, 2006
    #4
  5. Guest

    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.
     
    , Jan 22, 2006
    #5
  6. Rolf Magnus Guest

    Mike Wahler wrote:

    > 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.
     
    Rolf Magnus, Jan 23, 2006
    #6
  7. Mike Wahler Guest

    "Rolf Magnus" <> wrote in message
    news:dr29tr$2nj$02$-online.com...
    > Mike Wahler wrote:
    >
    >> 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.


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

    -Mike
     
    Mike Wahler, Jan 23, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pierre
    Replies:
    1
    Views:
    335
    Scott Allen
    Nov 13, 2004
  2. solartimba
    Replies:
    15
    Views:
    782
    red floyd
    Oct 20, 2003
  3. Replies:
    1
    Views:
    590
    Oliver Wong
    Jul 13, 2006
  4. \A_Michigan_User\

    Various path/path/files.asp all #include this 1 file

    \A_Michigan_User\, Jul 10, 2005, in forum: ASP General
    Replies:
    1
    Views:
    231
    Jeff Cochran
    Jul 10, 2005
  5. Schüle Daniel

    File::open and File.open

    Schüle Daniel, Oct 23, 2006, in forum: Ruby
    Replies:
    4
    Views:
    192
    Rick DeNatale
    Oct 24, 2006
Loading...

Share This Page