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

    getting files in directory

    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.

    FILE *name;
    name = fopen ("filelist.txt", "w+");
         if (name == NULL)
              printf("an error occured\n");
    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
    Visalia, CA, USA
    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")

    #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);
        while((dp = readdir(dfd)) != NULL) {
            if(!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
            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);
    void fsize(char *name) {
        struct stat stbuf;
        if(stat(name, &stbuf) == -1) {
            fprintf(stderr, "fsize: can't access %s\r\n", name);
        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);
    int main(int argc, char** argv) {
        if(argc != 2) {
            printf("whatever.exe directory");
            return 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

    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?

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

    thanks for any help


  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    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