Solved

Check whether a directory exists

Posted on 2004-04-15
8
935 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 3

Expert Comment

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

Expert Comment

by:anupvijay
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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 and use structures 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.

743 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

15 Experts available now in Live!

Get 1:1 Help Now