Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Check whether a directory exists

Posted on 2004-04-15
8
942 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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 and use conditional statements in the C programming language.

809 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