getting files in directory

This is a discussion on getting files in directory within the C Programming forums, part of the General Programming Boards category; hi i was wondering how to read the files in a directory. i thought first of all i have to ...

  1. #1
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214

    getting files in directory

    hi
    i was wondering how to read the files in a directory.

    i thought first of all i have to create a file where the files can be written to.

    Code:
    FILE *name;
    name = fopen ("filelist.txt", "w+");
         if (name == NULL)
              printf("an error occured\n");
         else
              ...............
         fclose(name);
    is there a command that checks the location of the file and writes down the files located there i.e. into a text file.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    There is actually an ansi way of doing this. You should look into dirent.h. Try something like this out:

    (NOTE: much of this was from "The C Programming Language")

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <fcntl.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <dirent.h>
    
    void dirwalk(char *dir, void (*fcn)(char *)) {
        char name[1024];
        struct dirent *dp;
        DIR *dfd;
    
        if((dfd = opendir(dir)) == NULL) {
            fprintf(stderr, "dirwalk: can't open %s\r\n", dir);
            return;
        }
        while((dp = readdir(dfd)) != NULL) {
            if(!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
                continue;
    
            if(strlen(dir) + strlen(dp->d_name)+2 > sizeof(name))
                fprintf(stderr, "dirwalk: name %s/%s too long\r\n", dir, dp->d_name);
            else {
                sprintf(name, "%s/%s", dir, dp->d_name);
                (*fcn)(name);
            }
        }
        closedir(dfd);
    }
    
    void fsize(char *name) {
        struct stat stbuf;
    
        if(stat(name, &stbuf) == -1) {
            fprintf(stderr, "fsize: can't access %s\r\n", name);
            return;
        }
    
        if((stbuf.st_mode & S_IFMT) == S_IFDIR) {
            dirwalk(name, fsize);
            printf("%s/\n", name);
        } else {
            printf("%8ld %s\r\n", stbuf.st_size, name);
            puts(name);
        }
    }
    
    int main(int argc, char** argv) {
        if(argc != 2) {
            printf("whatever.exe directory");
            return 1;
        }
        fsize(argv[1]);
    
        return 1;
    }
    If there is something wrong with the code (probably) just remember that this is old code that I just pulled out of a very old project and made a couple of quick changes to.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    23
    Hi,

    I saw this program and decided to try to write a pretty printer for directory listings. I wrote this recursive function but for some reason it only recurses down through one directory?

    Code:
     
    void recursive_dir(DIR* directory,int tabs){ 
       
      if(!(directory)) { 
        error("Directory is not accesible"); 
        return; 
      } 
       
      struct dirent *dip; 
     
      while(( dip = readdir( directory ))){ 
        if(!strcmp(dip->d_name, ".") || !strcmp(dip->d_name, "..")) 
          continue; 
         
        DIR* d = opendir(dip->d_name); 
        if(d != NULL){ 
          cout << "|-**"; 
          for(int i=0;i<tabs;i++)  // Print out the tabs 
    	cout << "---"; 
          cout << dip->d_name << "\n"; 
          recursive_dir(d,tabs+1); 
          closedir( d ); 
          continue; 
        } 
        
        for(int i=0;i<tabs;i++)  // Print out the tabs 
          cout << "   "; 
         
        cout << "|- " << dip->d_name << "\n";   
      } 
       
      return; 
    }
    Think I may have used opendir() wrong but cant see where,

    thanks for any help

    Brif

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    brif: Your code is C++, please post on the appropriate board.

    master5001: Which part of your code is part of the ANSI standard then? Most of it is POSIX stuff.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading .dat files from a folder in current directory...
    By porsche911nfs in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2009, 10:52 PM
  2. All files in a directory
    By cloudy in forum Linux Programming
    Replies: 1
    Last Post: 06-09-2008, 02:50 AM
  3. Couple errors please help :-D
    By JJJIrish05 in forum C Programming
    Replies: 9
    Last Post: 03-06-2008, 02:54 AM
  4. Listing files in a directory.
    By Devil Panther in forum C++ Programming
    Replies: 5
    Last Post: 11-04-2006, 09:39 PM
  5. Reading files in a directory
    By roktsyntst in forum Windows Programming
    Replies: 5
    Last Post: 02-07-2003, 10:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21