How to count the number files and sub directories of a directory with C

1 Answer

0 votes
#include <stdio.h>
#include <dirent.h>
    
int main(int argc, char **argv) 
{ 
    DIR *dir;
    struct dirent *file;
     
    if ( (dir = opendir("c:\\xampp\\htdocs\\allonpage.com")) == NULL) {
        puts("Error read directory");
        return 1;
    }
	int counter = 0;
    while ((file = readdir(dir)) != NULL) {
		counter++;
	}
 
    closedir(dir);
	
	printf("%d\n", counter);
     
    return 0;
}
    
/*
   
run:
    
132
 
*/

 



answered Jan 21, 2019 by avibootz
edited Jan 21, 2019 by avibootz
...