browse: forums | FAQ
Connecting Tech Pros Worldwide

Hey there! Do you need C / C++ help?

Get answers from our community of C / C++ experts on BYTES! It's free.

How do I check for directory existence in Linux?

hectorlas@yahoo.com
Guest
 
Posts: n/a
#1: Nov 15 '05
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




Alexei A. Frounze
Guest
 
Posts: n/a
#2: Nov 15 '05

re: How do I check for directory existence in Linux?


<hectorlas@yahoo.com> wrote in message
news:1127946407.908275.242790@z14g2000cwz.googlegr oups.com...[color=blue]
> 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.[/color]

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


Keith Thompson
Guest
 
Posts: n/a
#3: Nov 15 '05

re: How do I check for directory existence in Linux?


hectorlas@yahoo.com writes:[color=blue]
> 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.[/color]

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

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <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.
Anonymous 7843
Guest
 
Posts: n/a
#4: Nov 15 '05

re: How do I check for directory existence in Linux?


In article <1127946407.908275.242790@z14g2000cwz.googlegroups .com>,
<hectorlas@yahoo.com> wrote:[color=blue]
>
> 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.[/color]

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.
hectorlas@yahoo.com
Guest
 
Posts: n/a
#5: Nov 15 '05

re: How do I check for directory existence in Linux?


Sorry about the ot. I actually wised up right after posting (of
course) and used a chdir() error condition to see if it exists.

hectorlas@yahoo.com
Guest
 
Posts: n/a
#6: Nov 15 '05

re: How do I check for directory existence in Linux?


Sorry about the ot. I actually wised up right after posting (of
course) and used a chdir() error condition to see if it exists.

Igmar Palsenberg
Guest
 
Posts: n/a
#7: Nov 15 '05

re: How do I check for directory existence in Linux?


hectorlas@yahoo.com wrote:[color=blue]
> Sorry about the ot. I actually wised up right after posting (of
> course) and used a chdir() error condition to see if it exists.[/color]

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


Igmar
Closed Thread