How to check file exist using c language

Discussion in 'C Programming' started by andylcx@hotmail.com, Jul 6, 2005.

  1. Guest

    Hi all:
    The c++ language can check whether the file exist or not. I am
    wondering how c language does this job? Thanks a lot!

    Andy
     
    , Jul 6, 2005
    #1
    1. Advertising

  2. >Hi all:
    > The c++ language can check whether the file exist or not. I am
    >wondering how c language does this job? Thanks a lot!


    fopen()

    There is no absolutely correct way to check whether a file exists
    or not, since the security rules of the OS may not permit you
    to know whether the file exists or not. But fopen() in "r"
    mode gives you a definite YES or a probable NO.

    Gordon L. Burditt
     
    Gordon Burditt, Jul 6, 2005
    #2
    1. Advertising

  3. (Gordon Burditt) writes:
    >>Hi all:
    >> The c++ language can check whether the file exist or not. I am
    >>wondering how c language does this job? Thanks a lot!

    >
    > fopen()
    >
    > There is no absolutely correct way to check whether a file exists
    > or not, since the security rules of the OS may not permit you
    > to know whether the file exists or not. But fopen() in "r"
    > mode gives you a definite YES or a probable NO.


    On the other hand, the information may not be particularly useful.
    You can write a function that attempts to fopen() a file, records
    whether it succeeded, fclose()s the file, and returns the result of
    the fopen(). The file may no longer exist by the time you try to use
    it. For that matter, an fopen() call has to specify whether the file
    is to be opened in text or binary mode. If the OS makes a strong
    distinction between text and binary files, it may be that one will
    succeed and the other will fail. And, of course, knowing that a file
    exists doesn't guarantee that you can do anything with it.

    Presumably you want to know whether a file exists so you can find out
    whether you can do something with it. Often a better approach is to
    go ahead and try to do what you wanted to do in the first place, and
    handle any errors that result.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jul 6, 2005
    #3
  4. Malcolm Guest

    "Gordon Burditt" <> wrote
    > There is no absolutely correct way to check whether a file exists
    > or not, since the security rules of the OS may not permit you
    > to know whether the file exists or not. But fopen() in "r"
    > mode gives you a definite YES or a probable NO.
    >

    However if you close the file, and then try to open it again in the next
    statement, it may have disappeared.
     
    Malcolm, Jul 7, 2005
    #4
  5. Guest

    Is there any example availabe for my reference? Thanks a lot

    Malcolm wrote:
    > "Gordon Burditt" <> wrote
    > > There is no absolutely correct way to check whether a file exists
    > > or not, since the security rules of the OS may not permit you
    > > to know whether the file exists or not. But fopen() in "r"
    > > mode gives you a definite YES or a probable NO.
    > >

    > However if you close the file, and then try to open it again in the next
    > statement, it may have disappeared.
     
    , Jul 7, 2005
    #5
  6. Jack Klein Guest

    On 6 Jul 2005 13:55:38 -0700, wrote in
    comp.lang.c:

    > Hi all:
    > The c++ language can check whether the file exist or not.


    No, it cannot. And neither can the C++ standard library, which is
    much more pertinent. The person who told you this is mistaken. Even
    if you yourself are that person.

    > I am
    > wondering how c language does this job? Thanks a lot!


    C does not do this job, neither the language nor the standard library.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Jul 7, 2005
    #6
  7. writes:
    > Malcolm wrote:
    >> "Gordon Burditt" <> wrote
    >> > There is no absolutely correct way to check whether a file exists
    >> > or not, since the security rules of the OS may not permit you
    >> > to know whether the file exists or not. But fopen() in "r"
    >> > mode gives you a definite YES or a probable NO.
    >> >

    >> However if you close the file, and then try to open it again in the next
    >> statement, it may have disappeared.

    >
    > Is there any example availabe for my reference? Thanks a lot


    Please don't top-post. I've corrected it here.

    What kind of example do you have in mind?

    If a program opens a file, then closes it, then opens the same file
    again, there must be some finite time between closing it and
    re-opening it. Another program running on the same system could
    delete the file during that gap. (This assumes a system on which
    multiple programs can execute simultaneously.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jul 7, 2005
    #7
  8. Malcolm Guest

    "Keith Thompson" <> wrote
    > If a program opens a file, then closes it, then opens the same file
    > again, there must be some finite time between closing it and
    > re-opening it. Another program running on the same system could
    > delete the file during that gap. (This assumes a system on which
    > multiple programs can execute simultaneously.)
    >

    Or a system with a floppy. If the user presses the eject button, you might
    just get unlucky. Probably not worth bothering about for a video game, but
    if the program is dealing with medical data it wouldn't be acceptable.
     
    Malcolm, Jul 7, 2005
    #8
    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. Dave
    Replies:
    3
    Views:
    3,120
  2. Y.S.
    Replies:
    3
    Views:
    1,023
    strajan
    Sep 17, 2003
  3. LT
    Replies:
    7
    Views:
    2,099
    Phlip
    Jul 25, 2004
  4. Lin Ma
    Replies:
    2
    Views:
    322
    Lin Ma
    Jun 10, 2004
  5. Markus Mohr
    Replies:
    7
    Views:
    250
    Thomas 'PointedEars' Lahn
    Nov 28, 2003
Loading...

Share This Page