• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 984
  • Last Modified:

Check whether a directory exists

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
santoshv25
Asked:
santoshv25
1 Solution
 
avizitCommented:
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
 
akshayxxCommented:
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
 
ankuratvbCommented:
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
Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

 
akalmaniCommented:
u can use _access() too it returns ENOENT Filename or path not found.
0
 
anupvijayCommented:
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
 
NovaDenizenCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Tackle projects and never again get stuck behind a technical roadblock.
Join Now