How do I check for directory existence in Linux?

Discussion in 'C Programming' started by hectorlas@yahoo.com, Sep 28, 2005.

  1. Guest

    I wasn't sure if this should go in a linux group or the C group, but
    here I am.

    How do I check for directory existence in C? It's gcc under Linux to
    be exact, and I don't have the option of just making it to ensure that
    it's there.

    Thanks.
    Hector
     
    , Sep 28, 2005
    #1
    1. Advertising

  2. <> wrote in message
    news:...
    > How do I check for directory existence in C? It's gcc under Linux to
    > be exact, and I don't have the option of just making it to ensure that
    > it's there.


    This is a bit OT in this group since directory I/O is beyond the scope of
    the standard C library, but you may consider using opendir(), closedir(),
    readdir(). Download and use The Single UNIX Specification, it's free
    somewhere on the net. Or use man.

    Alex
     
    Alexei A. Frounze, Sep 28, 2005
    #2
    1. Advertising

  3. writes:
    > I wasn't sure if this should go in a linux group or the C group, but
    > here I am.
    >
    > How do I check for directory existence in C? It's gcc under Linux to
    > be exact, and I don't have the option of just making it to ensure that
    > it's there.


    Definitely a Linux or Unix group, probably comp.unix.programmer.
    Standard C has no concept of directories.

    --
    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, Sep 28, 2005
    #3
  4. In article <>,
    <> wrote:
    >
    > I wasn't sure if this should go in a linux group or the C group, but
    > here I am.
    >
    > How do I check for directory existence in C? It's gcc under Linux to
    > be exact, and I don't have the option of just making it to ensure that
    > it's there.


    There are posix-specific functions that will let you do
    that directly. opendir/readdir and stat come to mind.
    But pure C itself doesn't really have any directory-related
    functions.

    A partial hack is to try to open a file in the directory.

    If you can open a file in the directory, then the
    directory must exist. However the converse is not
    always true: if you cannot open the file, it may be
    for other reasons like access permission, disk full,
    running out of file descriptors, all kinds of stuff.
     
    Anonymous 7843, Sep 29, 2005
    #4
  5. Guest

    Sorry about the ot. I actually wised up right after posting (of
    course) and used a chdir() error condition to see if it exists.
     
    , Sep 29, 2005
    #5
  6. Guest

    Sorry about the ot. I actually wised up right after posting (of
    course) and used a chdir() error condition to see if it exists.
     
    , Sep 29, 2005
    #6
  7. wrote:
    > Sorry about the ot. I actually wised up right after posting (of
    > course) and used a chdir() error condition to see if it exists.


    man 2 stat, since chdir() does change the working dir if it succeeds.


    Igmar
     
    Igmar Palsenberg, Sep 29, 2005
    #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. Leon
    Replies:
    1
    Views:
    444
    John Timney \(Microsoft MVP\)
    Oct 18, 2003
  2. TaeHo Yoo
    Replies:
    3
    Views:
    5,995
    John Timney \(Microsoft MVP\)
    Oct 20, 2003
  3. CK
    Replies:
    5
    Views:
    24,354
    Lars-Inge T√łnnessen \(VJ# MVP\)
    Mar 21, 2006
  4. Replies:
    3
    Views:
    2,987
    Gordon Beaton
    Apr 17, 2006
  5. Thomas Scheffler

    XSLT: check existence of a variable

    Thomas Scheffler, Sep 17, 2003, in forum: XML
    Replies:
    0
    Views:
    708
    Thomas Scheffler
    Sep 17, 2003
Loading...

Share This Page