Search Contact information
University of Cambridge Home Department of Engineering
University of Cambridge >  Engineering Department >  computing help
next up previous contents
Next: Calling fortan NAG library Up: Examples Previous: A data filter   Contents

Reading Directories

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

#define REQUEST_DIR "/"
int main(int argc, char *argv[]){

FILE *fp;
DIR *dirp;
struct dirent *dp;
struct stat buf;

     dirp = opendir(REQUEST_DIR);
     /* Look at each entry in turn */
     while ((dp = readdir(dirp)) != NULL) {

       /* Now stat the file to get more information */
       if (stat(dp->d_name, &buf) == -1)

       if (S_ISDIR(buf.st_mode))
          printf("%s is a directory\n", dp->d_name);
       else if  (S_ISREG(buf.st_mode))     
          printf("%s is a regular file\n", dp->d_name);
     (void) closedir(dirp);

Tim Love 2010-04-27