Solved

Details of a file.

Posted on 2000-03-06
8
185 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
  • 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 15 total points
ID: 2587992
try S_IFDIR instead, it is defined
in <sys/stat.h>
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

778 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