Solved

Check whether a directory exists

Posted on 2004-04-15
8
938 Views
Last Modified: 2010-07-27
I am writing a c program
in which i am accepting command parameter as directoyr name

and i want to check whether that directory exists.
how to check through the program ?? only using system command ?
any other alternative ?

thanks
0
Comment
Question by:santoshv25
8 Comments
 
LVL 11

Accepted Solution

by:
avizit earned 30 total points
ID: 10839049
use the opendir() function


NAME

       opendir - open a directory



SYNOPSIS

       #include <sys/types.h>

       #include <dirent.h>

       DIR *opendir(const char *name);


DESCRIPTION

       The  opendir()  function  opens  a directory stream corre-
       sponding to the directory name, and returns a  pointer  to
       the  directory  stream.   The  stream is positioned at the
       first entry in the directory.



RETURN VALUE

       The opendir() function returns a pointer to the  directory
       stream or NULL if an error occurred.



ERRORS

       EACESS Permission denied.

       EMFILE Too many file descriptors in use by process.

       ENFILE Too many files are currently open in the system.

       ENOENT Directory  does  not  exist,  or  name  is an empty
              string.

       ENOMEM Insufficient memory to complete the operation.

       ENOTDIR
              name is not a directory.


0
 
LVL 8

Expert Comment

by:akshayxx
ID: 10839353
another way can be using
"stat"
http://www.hmug.org/man/2/stat.html

if it returns the error "ENOENT" that means the directory doestnt exist..

opendir sounds more descriptive that u r checking on a directory.. stat works for any file/dir
akshay
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10840509
You can use chdir() if you are using Boraland COmpilers(turboc++ or Borland C++)

You require dir.h for this.

chdir returns an int.
If directory doesnt exist,it returns -1 and sets errno to ENOENT.

0
Why spend so long doing email signature updates?

Do you spend loads of your time carrying out email signature updates? Not very interesting are they? Don’t let signature updates get you down. Let Exclaimer Cloud - Signatures for Office 365 make managing email signatures a breeze.

 
LVL 3

Expert Comment

by:akalmani
ID: 10841200
u can use _access() too it returns ENOENT Filename or path not found.
0
 
LVL 2

Expert Comment

by:anupvijay
ID: 10842369
Another method, if you are on windows/MSVC.

DWORD FA = GetFileAttributes(path);
 if (FA != (DWORD) -1) {
  // The path exists
  if (FA & FILE_ATTRIBUTE_DIRECTORY) {
   // It's a directory
  }
 }


But the truth is that there are a number of correct ways of doing the same thing such as this. It is your choice really.

Cheers.
0
 
LVL 22

Expert Comment

by:NovaDenizen
ID: 10843272
Best, fastest, most reliable way is to use stat.  run "man 2 stat" to see the man page.

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

....
struct stat mystat;
int res;

res = stat(filename, &mystat);
if (res != 0) {
    printf("can't stat %s:  %s\n", filename, strerror(errno));
} else if (S_ISDIR(mystat.st_mode)) {  // or use mystat.st_mode & 0x4000
    printf("Yep, it's a directory\n");
} else {
    printf("It exists, but it's not a directory\n");
}
0

Featured Post

The curse of the end user strikes again      

You’ve updated all your end user’s email signatures. Hooray! But guess what? They’re playing around with the HTML, adding stupid taglines and ruining the imagery. Find out how you can save your signatures from end users today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now