How to check file exists.

Discussion in 'C Programming' started by Raymond, May 27, 2005.

  1. Raymond

    Raymond Guest

    Hi All:

    To find a file exists using the file name, I have tow routings on UNIX
    system.

    1. access(2)
    2. lstat(2)

    This tow function also can do. When the return value is "-1" and errno
    is "ENOENT", The file doesn't exist.

    **Which one is better?

    Thank you very much.
    Raymond Shen.
    Raymond, May 27, 2005
    #1
    1. Advertising

  2. Raymond

    Jack Klein Guest

    On Fri, 27 May 2005 11:35:43 +0800, Raymond <>
    wrote in comp.lang.c:

    > Hi All:
    >
    > To find a file exists using the file name, I have tow routings on UNIX
    > system.
    >
    > 1. access(2)
    > 2. lstat(2)
    >
    > This tow function also can do. When the return value is "-1" and errno
    > is "ENOENT", The file doesn't exist.
    >
    > **Which one is better?
    >
    > Thank you very much.
    > Raymond Shen.


    Neither, because neither of them is part of standard C, so they are
    off-topic here. If you want a comparison of non-standard extensions
    provided by UNIX, ask in news:comp.unix.programmer.

    --
    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, May 27, 2005
    #2
    1. Advertising

  3. >To find a file exists using the file name, I have tow routings on UNIX
    >system.
    >
    >1. access(2)
    >2. lstat(2)
    >
    >This tow function also can do. When the return value is "-1" and errno
    >is "ENOENT", The file doesn't exist.
    >
    >**Which one is better?


    fopen() has the advantage of existing in standard C, and if
    it succeeds, the file exists and you can access it.

    All 3 functions have different ways in which they can give an
    indecisive answer:

    - lstat() can give a false positive, depending on what you think about
    the situation, if the name refers to a broken symlink.
    - access() can give a false positive or negative due to permissions
    issues if the effective and real user ids are different.
    - All 3 can fail with errno == EPERM and you don't know whether the
    file exists or not, because you don't have permission to know that.
    - fopen() fails on files which exist but you don't have permission
    to use the mode (read or write) which you asked for.

    Gordon L. Burditt
    Gordon Burditt, May 27, 2005
    #3
  4. Raymond

    Raymond Guest

    Gordon Burditt wrote:
    >>To find a file exists using the file name, I have tow routings on UNIX
    >>system.
    >>
    >>1. access(2)
    >>2. lstat(2)
    >>
    >>This tow function also can do. When the return value is "-1" and errno
    >>is "ENOENT", The file doesn't exist.
    >>
    >>**Which one is better?

    >
    >
    > fopen() has the advantage of existing in standard C, and if
    > it succeeds, the file exists and you can access it.


    Thank you Gordon.
    But I only want to know whether the file exists. If fopen success, The
    file is open and a system file handler is assigned. I must close the
    handler. Consider the performance of the application. I'd like use a
    lighter routing.

    >
    > All 3 functions have different ways in which they can give an
    > indecisive answer:
    >
    > - lstat() can give a false positive, depending on what you think about
    > the situation, if the name refers to a broken symlink.
    > - access() can give a false positive or negative due to permissions
    > issues if the effective and real user ids are different.
    > - All 3 can fail with errno == EPERM and you don't know whether the
    > file exists or not, because you don't have permission to know that.
    > - fopen() fails on files which exist but you don't have permission
    > to use the mode (read or write) which you asked for.
    >
    > Gordon L. Burditt
    Raymond, May 27, 2005
    #4
  5. Raymond

    Alan Balmer Guest

    On Fri, 27 May 2005 13:45:17 +0800, Raymond <>
    wrote:
    >Gordon Burditt wrote:
    >> fopen() has the advantage of existing in standard C, and if
    >> it succeeds, the file exists and you can access it.

    >
    >Thank you Gordon.
    >But I only want to know whether the file exists. If fopen success, The
    >file is open and a system file handler is assigned. I must close the
    >handler. Consider the performance of the application. I'd like use a
    >lighter routing.


    Then, please go ask in comp.unix.programming, as previously suggested.
    The question is on-topic there, and you will get more and better
    answers.

    --
    Al Balmer
    Balmer Consulting
    Alan Balmer, May 27, 2005
    #5
  6. Hello,

    Alan Balmer <> wrote:

    > On Fri, 27 May 2005 13:45:17 +0800, Raymond <>
    > wrote:
    >>Gordon Burditt wrote:
    >>> fopen() has the advantage of existing in standard C, and if
    >>> it succeeds, the file exists and you can access it.


    [...]

    > Then, please go ask in comp.unix.programming, as previously suggested.
    > The question is on-topic there, and you will get more and better
    > answers.


    Why isn't the question OnT here? I reformulate his question (or, at
    least, this is mine now ;-)):

    Is there a STANDARD way - that is, using only ANSI C - to determine if a
    file exists, without creating a new one?

    I fail to see why this is OT here.

    Regards,
    Spiro.

    --
    Spiro R. Trikaliotis http://cbm4win.sf.net/
    http://www.trikaliotis.net/ http://www.viceteam.org/
    Spiro Trikaliotis, May 27, 2005
    #6
  7. >>>> fopen() has the advantage of existing in standard C, and if
    >>>> it succeeds, the file exists and you can access it.

    >
    >[...]
    >
    >> Then, please go ask in comp.unix.programming, as previously suggested.
    >> The question is on-topic there, and you will get more and better
    >> answers.

    >
    >Why isn't the question OnT here? I reformulate his question (or, at
    >least, this is mine now ;-)):
    >
    >Is there a STANDARD way - that is, using only ANSI C - to determine if a
    >file exists, without creating a new one?


    Yes. fopen(filename, "r") .

    >I fail to see why this is OT here.


    The original post brought up functions that are not ANSI C:
    lstat() and access().

    Gordon L. Burditt
    Gordon Burditt, May 27, 2005
    #7
  8. Spiro Trikaliotis <> writes:
    > Alan Balmer <> wrote:
    >
    >> On Fri, 27 May 2005 13:45:17 +0800, Raymond <>
    >> wrote:
    >>>Gordon Burditt wrote:
    >>>> fopen() has the advantage of existing in standard C, and if
    >>>> it succeeds, the file exists and you can access it.

    >
    > [...]
    >
    >> Then, please go ask in comp.unix.programming, as previously suggested.
    >> The question is on-topic there, and you will get more and better
    >> answers.

    >
    > Why isn't the question OnT here?


    Raymond already said that fopen() is not satisfactory for his
    purposes, so he needs a system-specific solution.

    > I reformulate his question (or, at
    > least, this is mine now ;-)):
    >
    > Is there a STANDARD way - that is, using only ANSI C - to determine if a
    > file exists, without creating a new one?


    The best standard way to do this is to use fopen() and check whether
    it succeeded (and then call fclose() if it didn't). This has some
    drawbacks in some cases.

    (Depending on the system, it can sometimes be impossible to determine
    whether a file exists, for example if you don't have read access to
    the directory containing the file.)

    --
    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, May 27, 2005
    #8
  9. Raymond

    Alan Balmer Guest

    On Fri, 27 May 2005 18:55:10 +0200, Spiro Trikaliotis
    <> wrote:

    >Hello,
    >
    >Alan Balmer <> wrote:
    >
    >> On Fri, 27 May 2005 13:45:17 +0800, Raymond <>
    >> wrote:
    >>>Gordon Burditt wrote:
    >>>> fopen() has the advantage of existing in standard C, and if
    >>>> it succeeds, the file exists and you can access it.

    >
    >[...]
    >
    >> Then, please go ask in comp.unix.programming, as previously suggested.
    >> The question is on-topic there, and you will get more and better
    >> answers.

    >
    >Why isn't the question OnT here? I reformulate his question (or, at
    >least, this is mine now ;-)):
    >
    >Is there a STANDARD way - that is, using only ANSI C - to determine if a
    >file exists, without creating a new one?
    >
    >I fail to see why this is OT here.
    >

    That question, regarding a standard way, has already been answered.

    The answer was unsatisfactory to the OP, so he is directed to a group
    where more satisfactory answers are topical.

    --
    Al Balmer
    Balmer Consulting
    Alan Balmer, May 27, 2005
    #9
  10. Hello,

    I'll answer only Gordon here, but I thank the other posters, too.

    Gordon Burditt <> wrote:

    >>Is there a STANDARD way - that is, using only ANSI C - to determine if
    >>a file exists, without creating a new one?

    >
    > Yes. fopen(filename, "r")


    Oh yes. I'm sorry, it seems I did not read (or think about) the first
    answers here correctly. My mistake!

    Thanks,
    Spiro.

    --
    Spiro R. Trikaliotis http://cbm4win.sf.net/
    http://www.trikaliotis.net/ http://www.viceteam.org/
    Spiro Trikaliotis, May 28, 2005
    #10
  11. Raymond wrote on 27/05/05 :
    > But I only want to know whether the file exists. If fopen success, The file
    > is open and a system file handler is assigned. I must close the handler.
    > Consider the performance of the application. I'd like use a lighter routing.


    Design issue. Why do you want to know wether the file 'exist' if it's
    not to open if in some way ?

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    "Mal nommer les choses c'est ajouter du malheur au
    monde." -- Albert Camus.
    Emmanuel Delahaye, May 28, 2005
    #11
    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. Galina Grechka

    check if file exists on user machine

    Galina Grechka, Jan 12, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    413
    Galina Grechka
    Jan 13, 2005
  2. Sugapablo
    Replies:
    1
    Views:
    17,238
  3. Totan
    Replies:
    0
    Views:
    937
    Totan
    Apr 17, 2006
  4. Jason Stacy
    Replies:
    2
    Views:
    4,683
    Roedy Green
    Apr 27, 2008
  5. Ulf Meinhardt
    Replies:
    8
    Views:
    6,141
Loading...

Share This Page