Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C function to determine if a directory exists

Posted on 2011-03-04
17
Medium Priority
?
424 Views
Last Modified: 2012-06-27
Using C (not C++):

I have often used the C function access() to determine if a file exists e.g

if (!access(filename, F_OK))
   printf("File exists);
else
  printf("File does NOT exist\n");

For my current purposes, I need to be able to tell if a particular SUBDIRECTORY exists.
e..g if I have a subdir "STEVE", and pass "STEVE" to access() it will return 0, but I need to be able to tell if "STEVE" is a subdir, rather than a file.  Is there a function that can help me out?

Thanks,
Steve


0
Comment
Question by:Stephen Kairys
[X]
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
  • 8
  • 7
  • 2
17 Comments
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 35040578
Hi Steve,

One of the stat() functions should do nicely.


GoodLuck,
Kent

#include <sys\stat.h>
#include <stdio.h>
#include <time.h>
int main(void)
{
   struct stat statbuf;
   int    handle;

//  int stat(const char *path, struct stat *statbuf); 
   stat ("c:\\mypath\\dirname", &statbuf);

   if (statbuf.st_mode & S_S_IFDIR)
      printf("Directory....\n");
}

Open in new window

0
 
LVL 32

Expert Comment

by:phoffric
ID: 35040587
you can use readdir and get the file stats:
    http://www.ibm.com/developerworks/aix/library/au-unix-readdir.html 

"The stat() function fills a struct stat structure with information about a specific file; if you've got a file descriptor instead of a file name, you can use the fstat() function instead."

•"S_ISDIR(mode) -- Is this a directory?"
•"S_ISREG(mode) -- Is this a regular file?"
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35040662
Didn't see your post Kent while getting the link and excerpting from it.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 4

Author Comment

by:Stephen Kairys
ID: 35040738
Kent,
Welk. I tried your code, but it seems that the identifier

S_S_IFDIR

is not included in my
sys\stat.h
 nor
sys\types.h

even though stat() is prototyped in stat.h.
Any idea what I'm doing wrong?

Thanks!
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35040752
Did you also try
•"S_ISDIR(mode) -- Is this a directory?"
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35040764
In listing 5, for example, is the LOC:
     if( S_ISDIR( entryInfo.st_mode ) ) {
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35040783
Or, just a typo: try S_IFDIR  instead.
    http://linux.die.net/man/2/stat
0
 
LVL 4

Author Comment

by:Stephen Kairys
ID: 35040788
>>
S_ISDIR(mode) -- Is this a directory
Not sure I understand this. What is mode?
I see in sys\stat.h that
#define S_ISDIR( m )      (((m) & S_IFMT) == S_IFDIR)
is a macro, but not sure what to pass to it.
Thanks
0
 
LVL 4

Author Comment

by:Stephen Kairys
ID: 35040802
Look like both S_IFDIR and S_ISDIR exist in stat.h

#define S_IFDIR       0040000 /* directory */
#define S_ISDIR( m )      (((m) & S_IFMT) == S_IFDIR)

Still..need to know..what do I pass in as the parameter "mode"?
Thanks.
0
 
LVL 4

Author Comment

by:Stephen Kairys
ID: 35040849
Phoffric:
I see the two links you posted are from UNIX and Linux. Do they apply to WIndows/DOS 16-bit programming?
Thanks
0
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 2000 total points
ID: 35040910
Hi Steve,

Sorry about the typo.

S_IFDIR is a constant.  This constant is a bit that flags the item as a directory.

S_ISDIR(m) is a macro.  You would pass statbut->st_mode to it.


I corrected the item and reposted the source code.

Give it a try,
Kent

#include <sys\stat.h>
#include <stdio.h>
#include <time.h>
int main(void)
{
   struct stat statbuf;
   int    handle;

//  int stat(const char *path, struct stat *statbuf); 
   stat ("c:\\mypath\\dirname", &statbuf);

   if (statbuf.st_mode & S_IFDIR)
      printf("Directory....\n");
}

Open in new window

0
 
LVL 4

Author Comment

by:Stephen Kairys
ID: 35040916
OK, I think I got it.

Just to be safe, I first use access() to determine if the file exists: Then I invoke stat...
Please do not assume this code is perfect. I did only cursory testing.

Thanks everyone!



 isdir-c.txt
0
 
LVL 4

Author Comment

by:Stephen Kairys
ID: 35040930
Actually I defer to Kent. You don't need to include <sys\types.h>.  Points to be awaeded shortly. Thanks!
0
 
LVL 4

Author Closing Comment

by:Stephen Kairys
ID: 35040956
Kent,
Thank you. Your solution had everything in one place so you get the points

phoffric: - As per the above, Kent got the points, but I still appreciate your help. If you feel that you should share in the points, feel free t respond; however, I won't be able to handle your request 'til at least next Thursday as I am going to be on a short vacation. Thanks for your efforts.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35040968
>> Do they apply to WIndows/DOS 16-bit programming
I didn't realize you were working in Windows/DOS 16-bit programming. And I don't have that product available to test, so I cannot be sure.

But, I tried the kdo's post in VS 2010 Express, and it works fine (with the typo corrected), so that is a good sign that it will work for you.

Since S_ISDIR is defined for you (it is not defined in sys\stat.h in VS 2010 Express), I think the below code should work for you since it works in Linux. However, you need to verify using your DOS-16 system.
if (statbuf.st_mode & S_IFDIR)  
      printf("Directory....\n");  
   if( S_ISDIR( statbuf.st_mode ) )
      printf("S_ISDIR works in Linux\n");

Open in new window

0
 
LVL 32

Expert Comment

by:phoffric
ID: 35040977
no problem w/ the points. Like I said, it was just a typo.
0
 
LVL 4

Author Comment

by:Stephen Kairys
ID: 35040997
Thanks.  Anyone who reads this question should realize that I did not try it on the Linux or Unix but n 16-bit DOS running under windows.
0

Featured Post

Industry Leaders: 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!

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…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops 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.

610 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