Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Check whether a directory exists

Posted on 2004-04-15
8
Medium Priority
?
961 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 120 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

972 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