Solved

Details of a file.

Posted on 2000-03-06
8
193 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 15 total points
ID: 2587992
try S_IFDIR instead, it is defined
in <sys/stat.h>
0
Independent Software Vendors: 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

691 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