?
Solved

Details of a file.

Posted on 2000-03-06
8
Medium Priority
?
200 Views
Last Modified: 2010-04-02
Hi, In an environ, gcc running under solaris, i need to check whether a file is a directory or a simple file. Could anyone let me know how to do this.

-gsbabu
0
Comment
Question by:gsbabu
[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
  • 5
  • 3
8 Comments
 

Expert Comment

by:ori_b
ID: 2587702
Try the following :

#include <sys/types.h>
#include <sys/stat.h>

struct stat buf;
         
if (stat("file_name", &buf) == 0) {
 if ((buf.st_mode & S_IFMT) == _IFDIR)  {
      // it is a directory
}
0
 

Author Comment

by:gsbabu
ID: 2587954
ori_b
>> f ((buf.st_mode & S_IFMT) == _IFDIR)  {

on compiling, i get _IFDIR undeclared, i don't know more about, where it could be defined. help.

-gsbabu
0
 

Accepted Solution

by:
ori_b earned 45 total points
ID: 2587992
try S_IFDIR instead, it is defined
in <sys/stat.h>
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!

 

Author Comment

by:gsbabu
ID: 2588047
thanks, it is ok, also could i know how to make this in a loop, that is if in a directory there are some entries, which could be plain file or directory, i need to loop for the number of entries in the directory. could you help me. thanks for your previous help, let me know if you want me to post this as a new question.

--gsbabu.
0
 

Expert Comment

by:ori_b
ID: 2588257
You can try this (if I understood your question correctly) :

int     DirLoop(const char* path) {
 
  DIR *dirp;
  struct dirent *direntp;

  char file_name[256];
 
  printf("opening directory %s\n",path);
 
  dirp = opendir( path );
  if (dirp == NULL) {
      printf("Error %s : %s\n", path,strerror(errno));
      return -1;
  }
 
  while ( (direntp = readdir( dirp )) != NULL ) {
      if (direntp == NULL) {
        printf("Erorr %s : %s \n", path,strerror(errno));
        return -1;
      }


    // skip the ./ and ../ files        
    if (direntp->d_name[0] != '.') {

        // build the full path
        strcpy (file_name,path);
        strcat (file_name,"/");
        strcat (file_name,direntp->d_name);

        struct stat buf;
        
        // Find file's type
        if (stat(file_name, &buf) == 0) {
            if ((buf.st_mode & S_IFMT) == S_IFDIR) {
              // it is a directory
              DirLoop(file_name);
            }

            else {
              // A regular file - Do What you want to ...
              printf("%s\n",file_name);
            }
        }

        else {
            printf("Fail to stat file %s : %s\n",file_name, strerror(errno ));
            return -1;
        }
      
      }
  }

 
  if (closedir( dirp ) == -1) {
      printf("Erorr closedir of %s : %s \n", path,strerror(errno));
      return -1;
  }

  return 0;
}

0
 

Expert Comment

by:ori_b
ID: 2588268
You will need these #include  :

#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

0
 

Author Comment

by:gsbabu
ID: 2591166

Thanks very much Master Ori, It really worked pretty well.

 -gsbabu
 ugnasu01@thiru.vetri.com
0
 

Expert Comment

by:ori_b
ID: 2591624
glad to help
0

Featured Post

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!

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

752 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