Thread: counting files

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    29

    Question counting files

    I would like to know of an easier way to count the number of files
    (not directories) within a given folder. I have created this function but it counts both folders and files:

    #include <stdio.h>
    #include <dirent.h>

    long file_count(char *directory)
    {
    DIR *dp;
    long file_count=0;
    struct dirent *ent;
    dp = opendir(directory); // dir to open
    while ( (ent = readdir(dp)) != NULL )
    {
    file_count++;
    }

    return file_count;
    }

    Any suggestions,

    Thanks,

    bigSteve

  2. #2
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Thumbs up

    If u are working in the DOS environment (or a DOS shell under Windows) then try the following...

    #include<stdio.h>
    #include<process.h>

    int main()
    {
    FILE *fp;
    int count,i;
    char ch;

    i=system("DIR /a-d /b >> filecnt.txt");
    if(i==0)
    {
    fp=fopen("filecnt.txt","r");
    if(fp==NULL)
    {
    printf("\nError Opening the Temporary File");
    exit(1);
    }
    count=0;
    while(!eof(fp))
    {
    ch=fgetc(fp);
    if(ch=='\n')
    count++;
    }
    printf("\n The Number of Files in the Directory is...%d",count);
    }
    else
    {
    printf("\nSYSTEM command is not working on ur system.");
    }
    return(0);
    }
    Help everyone you can

  3. #3
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Lightbulb

    Just forgot to close the opened file....Please add that line...

    fclose(fp);

    CHEERS,
    Harsha.
    Help everyone you can

  4. #4
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Lightbulb

    Just forgot to close the opened file....Please add that line...

    fclose(fp);

    Also. this program counts the files only in the current folder but not else where. Now, u can easily modify the program, esp the DIR statement if u want another directory.

    CHEERS,
    Harsha.
    Help everyone you can

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >while(!eof(fp))
    I think you meant while ( !feof ( fp ) ). Even then it's incorrect, you can search the boards for further details, it's been covered quite a bit. A better way is to declare ch as an int and do this:
    Code:
    while( ( ch = fgetc ( fp ) ) != EOF ) 
    {  
      if ( ch == '\n' ) 
      count++; 
    }
    You also might consider adding code to check if the file contains data and emptying it before doing anything else. You'll notice that if you run the program several times in succession the number of files keeps increasing because you don't clear the file.

    I also cut the time this program takes to run in half by declaring a buffer and using fgets instead of fgetc.
    Code:
    while( fgets ( buf, sizeof buf, fp ) != NULL ) 
      count++;
    -Prelude
    My best code is written with the delete key.

  6. #6
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Ehhh, I'm not sure what compiler you are using but maybe you can use the stat function.
    Code:
    #include <stdio.h> 
    #include <sys/stat.h>
    #include <dirent.h> 
    
    long file_count(char *directory) 
    { 
      DIR *dp;
      struct stat filestat;
      long file_count=0; 
      struct dirent *ent; 
    
      if((dp = opendir(directory)) == NULL) // dir to open 
        return -1;
    
      while ( (ent = readdir(dp)) != NULL ) 
      { 
        if(stat(dp->d_name, &filestat) >= 0) // don't know if you need the full path name
        {
          if(!(filestat.st_mode & S_IFDIR))
              file_count++; 
        }
      } 
    
      closedir(dp);
      return file_count; 
    }

  7. #7
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192
    >while(!eof(fp))
    I think you meant while ( !feof ( fp ) ). Even then it's incorrect, you can search the boards for further details, it's been covered quite a bit. A better way is to declare ch as an int and do this:

    code:--------------------------------------------------------------------------------
    while( ( ch = fgetc ( fp ) ) != EOF )
    {
    if ( ch == '\n' )
    count++;
    }
    --------------------------------------------------------------------------------
    Yes, I meant eof(fp) only, but the thing is it is declared in io.h. So, need to #include it.

    Good point. I missed it. U should empty it. But all that effort can be saved just by changing the dos command from

    dir /a-d /b >> filecnt.txt to

    dir /a-d /b > filecnt.txt


    That would overwrite the file and would contain only the new list.
    I thought of fgets() but just played safe....

    Regards,
    Sriharsha
    Help everyone you can

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Create Copies of Files
    By Kanshu in forum C++ Programming
    Replies: 13
    Last Post: 05-09-2009, 07:53 AM
  2. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  3. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  4. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  5. Counting Files
    By groovy in forum C Programming
    Replies: 9
    Last Post: 10-05-2005, 11:22 AM