How do I check for directory existence in Linux?

H

hectorlas

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
 
A

Alexei A. Frounze

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
 
K

Keith Thompson

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.
 
A

Anonymous 7843

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.
 
H

hectorlas

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

hectorlas

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

Igmar Palsenberg

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top