Check whether a file exists

Discussion in 'C++' started by sword, Jul 30, 2006.

  1. sword

    sword Guest

    How can I check whether a file exists?
    sword, Jul 30, 2006
    #1
    1. Advertising

  2. "sword" <> wrote in message
    news:...
    : How can I check whether a file exists?

    You'll get the same answer here as in the C NG ;)

    The portable way to check for this is to try to open
    the file (you may use ifstream, or fopen as in C)
    to check if it succeeds, then close it immediately.

    Two main issues are:
    - various operating systems may have various
    restrictions on visibility or accessibility
    of files within a given storage/directory.
    - on a multitasking OS, a file that exists
    at a given time may be deleted by the next
    statement.

    So in general, best is to just try to use the file,
    and report the error at that point.


    hth-Ivan
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
    Brainbench MVP for C++ <> http://www.brainbench.com
    Ivan Vecerina, Jul 30, 2006
    #2
    1. Advertising

  3. "sword" <> schreef in bericht
    news:...
    > How can I check whether a file exists?
    >


    man access

    John
    Bruintje Beer, Jul 30, 2006
    #3
  4. sword

    sword Guest

    Thanks
    sword, Jul 30, 2006
    #4
  5. Bruintje Beer <> wrote:

    > "sword" <> schreef in bericht
    > news:...


    >> How can I check whether a file exists?


    > man access


    Are you aware of the fact this will not lead to any solution to the
    problem at all in many cases?
    --
    jb

    (reply address in rot13, unscramble first)
    Jakob Bieling, Jul 30, 2006
    #5
  6. sword

    Moonlit Guest

    Hi,

    On most platforms there is a function called fstat, stat and lstat that uses
    a filename, or descriptor and lets you check for various stuff about a file
    (I believe it returns
    '-1' if a file doesn't exist) Available on linux, solaris, windows, dec and
    probably many more.
    --


    Regards, Ron AF Greve

    http://moonlit.xs4all.nl

    "sword" <> wrote in message
    news:...
    > How can I check whether a file exists?
    >
    Moonlit, Jul 30, 2006
    #6
    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. OvErboRed
    Replies:
    2
    Views:
    577
    FeU Hagen
    May 22, 2004
  2. Fao, Sean

    Check Whether SharePoint Group Exists

    Fao, Sean, Sep 26, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    2,798
    eliza81
    Jun 21, 2010
  3. Jason Stacy
    Replies:
    2
    Views:
    4,640
    Roedy Green
    Apr 27, 2008
  4. Ulf Meinhardt
    Replies:
    8
    Views:
    6,089
  5. JB
    Replies:
    1
    Views:
    539
Loading...

Share This Page