Checkup: better way to test for file existence?

Discussion in 'C++' started by Matt, Feb 14, 2005.

  1. Matt

    Matt Guest

    Hello,

    I see other references in this newsgroup saying that the only standard
    C++ way to test for file existence is some variant of my code below;
    can someone please confirm...or offer alternatives?

    Additionally, might there be cross-platform alternatives, say in a
    library like Boost, or something else?

    -Matt

    #include <fstream>

    /*
    [class FileHandle contains 'fstream file' and previously-initialed
    'string filename'.]
    */

    bool
    FileHandle::exists()
    {
    bool retval = false;

    // TODO/XXX: Is there a better way to
    // test the existence of a file
    // without opening it?

    file.open(filename.c_str(), ios::in | ios::binary);

    if (!file.fail()) retval = true;

    file.close();

    return retval;
    }

    --
    Remove the "downwithspammers-" text to email me.
     
    Matt, Feb 14, 2005
    #1
    1. Advertising

  2. Matt

    Mike Wahler Guest

    "Matt" <> wrote in message
    news:...
    > Hello,
    >
    > I see other references in this newsgroup saying that the only standard
    > C++ way to test for file existence is some variant of my code below;
    > can someone please confirm...or offer alternatives?


    You must have misread them, or read some that were not
    correct. There is no way in standard C++ to detect
    conclusively the nonexistence of a file. (If an
    open succeeds when using a mode other than once which
    will create a nonexistent file, then of course that
    does indicate that the file previously existed). But
    if an open fails, that does not necessarily mean it
    doesn't exist. All you can determine is if an attempt
    to open a file succeeded or failed. An attempt to open
    could fail for a variety of reasons depending upon the
    environment, including but not limited to: insufficient
    OS permissions, device where file resides is offline, does
    not exist, etc. Many implementations do provide extensions
    for obtaining more detailed information about i/o, but
    they're not standard.

    >
    > Additionally, might there be cross-platform alternatives, say in a
    > library like Boost, or something else?


    There probably are some which address at least a small
    number of environments. Try google.


    >
    > -Matt
    >
    > #include <fstream>
    >
    > /*
    > [class FileHandle contains 'fstream file' and previously-initialed
    > 'string filename'.]
    > */
    >
    > bool
    > FileHandle::exists()
    > {
    > bool retval = false;
    >
    > // TODO/XXX: Is there a better way to
    > // test the existence of a file
    > // without opening it?


    Depends upon what 'better' means. There is no way
    at all using standard C++. AFAIK platform-specific
    solutions are provided by most implementations.

    >
    > file.open(filename.c_str(), ios::in | ios::binary);
    >
    > if (!file.fail()) retval = true;
    >
    > file.close();
    >
    > return retval;
    > }


    If this function returns 'false', all that means
    is that the file could not be opened (for an unknown
    reason). Nothing more, nothing less.

    -Mike
     
    Mike Wahler, Feb 14, 2005
    #2
    1. Advertising

  3. Matt

    Guest

    You can test this with an old school C function:

    #include "unistd.h."
    #include <iostream>
    #include <string>
    int main(int argc, char **argv)
    {
    int ret(0);
    std::string fname("C:\\AUTOEXEC.BAT");
    ret = access(fname.c_str(), F_OK);
    if (ret == -1)
    std::cout << "Access on file " << fname << " is denied" <<
    std::endl;

    system("Pause");
    return EXIT_SUCCESS;
    }

    more specific tests this if you can get access to the file.
     
    , Feb 14, 2005
    #3
  4. Matt

    Rolf Magnus Guest

    Matt wrote:

    > bool
    > FileHandle::exists()
    > {
    > bool retval = false;
    >
    > // TODO/XXX: Is there a better way to
    > // test the existence of a file
    > // without opening it?


    Why would you want to test for the existence of a file if you don't want to
    open it?
    If you just want to make sure it's there so you can open it later, that's
    not a good idea since something could change in between (file
    removed/created/renamed, permissions changed, device unmounted, whatever).
     
    Rolf Magnus, Feb 14, 2005
    #4
  5. Matt

    Mike Wahler Guest

    Re: [OT] Checkup: better way to test for file existence?

    > wrote in message
    news:...
    > You can test this with an old school C function:


    'access()' is not a C (or C++) function, it's an extension
    provided by some implementations. Also your use of it below
    does not indicate a guarantee of a file's existence or nonexistence,
    only whether it is accessible. An fstream object can already provide
    this information.

    >
    > #include "unistd.h."
    > #include <iostream>
    > #include <string>
    > int main(int argc, char **argv)
    > {
    > int ret(0);
    > std::string fname("C:\\AUTOEXEC.BAT");
    > ret = access(fname.c_str(), F_OK);
    > if (ret == -1)
    > std::cout << "Access on file " << fname << " is denied" <<
    > std::endl;
    >
    > system("Pause");
    > return EXIT_SUCCESS;
    > }
    >
    > more specific tests this if you can get access to the file.


    Here the topic is ISO standard C++. Your 'solution' is specific
    to a UNIX implementation, so is not applicable to other platforms,
    and is not topical here.

    -Mike
     
    Mike Wahler, Feb 14, 2005
    #5
  6. Matt

    Matt Guest

    On Mon, 14 Feb 2005 20:35:07 +0100, Rolf Magnus <>
    wrote:
    >Why would you want to test for the existence of a file if you don't want to
    >open it?


    I'm using (in an application I'm building to simulate a
    more-complicated, future design) the existence or non-existence of
    files as a means of inter-process communication.

    (eg, while one process is processing a certain event, it writes a
    zero-sized-file, then deletes said file when the event completes. A
    separate process polls the existence of said file to see if the event
    is continuing. I doubt I can give you many more details beyond that
    without explaining more in depth my application and prototype.)

    >If you just want to make sure it's there so you can open it later, that's
    >not a good idea since something could change in between (file
    >removed/created/renamed, permissions changed, device unmounted, whatever).


    Yes, obviously. However, that's not what I'm trying to do. See
    above.

    -Matt
    --
    Remove the "downwithspammers-" text to email me.
     
    Matt, Feb 14, 2005
    #6
  7. Matt

    Matt Guest

    On Mon, 14 Feb 2005 19:13:35 GMT, "Mike Wahler"
    <> wrote:

    >
    >"Matt" <> wrote in message
    >news:...
    >> Hello,
    >>
    >> I see other references in this newsgroup saying that the only standard
    >> C++ way to test for file existence is some variant of my code below;
    >> can someone please confirm...or offer alternatives?

    >
    >You must have misread them, or read some that were not
    >correct. There is no way in standard C++ to detect
    >conclusively the nonexistence of a file.


    Fair enough. I'll rephrase my question:

    What might you recommend as the best possible method/algorithm to
    detect the non-existence of a file (even though it may not catch all
    cases as noted below) in lieu of having a definitive means to do so
    accurately.

    As I read from the info below, there is no conclusive way to do this.
    Hence there is no real answer to the problem.

    What I'm looking for are alternative answers that get me "as close as
    possible."

    >> Additionally, might there be cross-platform alternatives, say in a
    >> library like Boost, or something else?

    >
    >There probably are some which address at least a small
    >number of environments. Try google.


    Thanks for the google tip. I have been looking, even prior to this
    post. My search was not initially productive, hence my post here.

    -Matt

    --
    Remove the "downwithspammers-" text to email me.
     
    Matt, Feb 14, 2005
    #7
  8. Matt

    Mike Wahler Guest

    "Matt" <> wrote in message
    news:...
    > On Mon, 14 Feb 2005 19:13:35 GMT, "Mike Wahler"
    > <> wrote:
    >
    > >
    > >"Matt" <> wrote in message
    > >news:...
    > >> Hello,
    > >>
    > >> I see other references in this newsgroup saying that the only standard
    > >> C++ way to test for file existence is some variant of my code below;
    > >> can someone please confirm...or offer alternatives?

    > >
    > >You must have misread them, or read some that were not
    > >correct. There is no way in standard C++ to detect
    > >conclusively the nonexistence of a file.

    >
    > Fair enough. I'll rephrase my question:
    >
    > What might you recommend as the best possible method/algorithm to
    > detect the non-existence of a file (even though it may not catch all
    > cases as noted below) in lieu of having a definitive means to do so
    > accurately.
    >
    > As I read from the info below, there is no conclusive way to do this.
    > Hence there is no real answer to the problem.
    >
    > What I'm looking for are alternative answers that get me "as close as
    > possible."


    If you open the file as "input only, don't create" then
    barring the circumstances I cited, a return of 'true'
    from 'istream::fail()' would probably mean 'not found'.

    std::ifstream input("filename");
    if(!input)
    std::cerr << "open failed, file probably does not exist\n";

    -Mike
     
    Mike Wahler, Feb 14, 2005
    #8
  9. Matt

    Matt Guest

    On Mon, 14 Feb 2005 11:58:04 -0600, Matt
    <> wrote:
    >Additionally, might there be cross-platform alternatives, say in a
    >library like Boost, or something else?


    I had a peak at Boost, there appears to be a file "exists()" method in
    its filesystem library:

    http://www.boost.org/libs/filesystem/doc/

    I may check this out and use it, not sure yet.

    -Matt
    --
    Remove the "downwithspammers-" text to email me.
     
    Matt, Feb 15, 2005
    #9
  10. You could try something like this:

    bool FileExists( std::string strFile )
    {
    bool bSuccess = true;
    struct _stat stFile;

    if( _stat( strFile.c_str(), &stFile ) == -1 )
    bSuccess = false;

    return bSuccess;
    }

    For linux, replace _stat with stat...
     
    sqaengineering, Feb 15, 2005
    #10
  11. Matt

    Matt Guest

    On 14 Feb 2005 18:43:33 -0800, "sqaengineering"
    <> wrote:
    > if( _stat( strFile.c_str(), &stFile ) == -1 )


    Excellent recommendation, I should have thought of this POSIX-based
    stat() function earlier.

    (It's been a long time since I've done system programming...it's
    starting to come back to me now...and I've dug up my beloved, purple
    "POSIX Programmer's Guide" book. Yes, I know this is not "standard"
    C++, but it seems to be the next best thing, and with cygwin/mingw on
    my Windows machines, I seem to be POSIX compliant on virtually every
    platform I choose...I hope?)

    -Matt

    On 14 Feb 2005 18:43:33 -0800, "sqaengineering"
    <> wrote:

    >You could try something like this:
    >
    >bool FileExists( std::string strFile )
    >{
    > bool bSuccess = true;
    > struct _stat stFile;
    >
    > if( _stat( strFile.c_str(), &stFile ) == -1 )
    > bSuccess = false;
    >
    > return bSuccess;
    >}
    >
    >For linux, replace _stat with stat...


    --
    Remove the "downwithspammers-" text to email me.
     
    Matt, Feb 15, 2005
    #11
  12. Matt

    Matt Guest

    Hi Mike, Thanks for this clarification. -Matt

    On Mon, 14 Feb 2005 22:39:27 GMT, "Mike Wahler"
    <> wrote:
    >If you open the file as "input only, don't create" then
    >barring the circumstances I cited, a return of 'true'
    >from 'istream::fail()' would probably mean 'not found'.
    >
    >std::ifstream input("filename");
    >if(!input)
    > std::cerr << "open failed, file probably does not exist\n";
    >
    >-Mike
    >


    --
    Remove the "downwithspammers-" text to email me.
     
    Matt, Feb 15, 2005
    #12
  13. Matt

    Guest

    Re: [OT] Checkup: better way to test for file existence?

    Point taken. It compile and executes on windows as well for code
    compiled with g++ though - so it is not strictly a unix call ....
     
    , Feb 16, 2005
    #13
  14. In message <cuqugo$3le$05$-online.com>, Rolf Magnus
    <> writes
    >Matt wrote:
    >
    >> bool
    >> FileHandle::exists()
    >> {
    >> bool retval = false;
    >>
    >> // TODO/XXX: Is there a better way to
    >> // test the existence of a file
    >> // without opening it?

    >
    >Why would you want to test for the existence of a file if you don't want to
    >open it?


    A program for producing lists of files springs to mind ;-)

    --
    Richard Herring
     
    Richard Herring, Feb 17, 2005
    #14
  15. Matt

    Rolf Magnus Guest

    Richard Herring wrote:

    > In message <cuqugo$3le$05$-online.com>, Rolf Magnus
    > <> writes
    >>Matt wrote:
    >>
    >>> bool
    >>> FileHandle::exists()
    >>> {
    >>> bool retval = false;
    >>>
    >>> // TODO/XXX: Is there a better way to
    >>> // test the existence of a file
    >>> // without opening it?

    >>
    >>Why would you want to test for the existence of a file if you don't want
    >>to open it?

    >
    > A program for producing lists of files springs to mind ;-)


    lol, you mean sort of a brute-force way to get a directory listing without
    actual directory support? I guess that'd take quite some time, even if you
    assume "good" old DOS with it's case insensitive 8.3 file names. :)
     
    Rolf Magnus, Feb 22, 2005
    #15
    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. bjam
    Replies:
    0
    Views:
    399
  2. puzzlecracker
    Replies:
    4
    Views:
    276
    Jim Langston
    Oct 31, 2005
  3. J Krugman
    Replies:
    2
    Views:
    114
    Alan J. Flavell
    Jul 8, 2003
  4. still just me

    Testing File Existence - best way?

    still just me, Dec 13, 2007, in forum: Perl Misc
    Replies:
    2
    Views:
    110
    Juha Laiho
    Dec 27, 2007
  5. pgodfrin
    Replies:
    6
    Views:
    95
    Ilya Zakharevich
    Jan 22, 2008
Loading...

Share This Page