Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 09-23-2003, 11:13 PM   #1
Registered: Sep 2003
Posts: 74

Rep: Reputation: 15

If i want to write a C program in counting the no of file in a folder, what should i do?
Old 09-24-2003, 12:09 AM   #2
Registered: Feb 2003
Location: Iran
Distribution: Solaris 10
Posts: 201

Rep: Reputation: 30
Here is borrowed from Mr.Ritchie
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define NAME_MAX 10

typedef struct {
  int fd;
  char name [NAME_MAX + 1];
} Dirent;

typedef struct {
  int fd;
  Dirent d;
} Dir;

/* that's just for portability (Mohsen)*/

/* Now you can use 3 functions: */

DIR *opendir (char* dirname);
Dirent *readdir (DIR *dfd);
void closedir (char*, struct stat*)  
The Dirent structure contains the inode 
number and the name. The maximium length of a file 
name component is NAME_MAX , which 
is a SYSTEM DEPENDENT value. opendir returns  
a pointer to a structure called DIR, 
analogous to FILE, which is used 
by readdir and closedir. 

Now you may use stat system call to determine if your name is a directory (take a look at manual page of stat).
Dirent *dp;
DIR *dfd;

if ((dfd = opendir ("MyDirName")) == NULL)
  exit (1);

/* readdir returns 
file names in directory. dp->name may be `.' 
or `..' which should be ignored. */
while ((dp = readdir (dfd) != NULL) {
  if (strcmp (dp->name, ".") == 0 || strcmp (dp->name, "..")   == 0)
  printf ("file: %s\n", dp->name);

I'va wrote it in a hurry so check out the man pages on any question.

Last edited by Mohsen; 09-24-2003 at 12:13 AM.
Old 09-24-2003, 12:59 AM   #3
Registered: Aug 2003
Location: Planet Earth
Distribution: Linux Mint Debian Edition (LMDE)
Posts: 215

Rep: Reputation: 31
Re: counting

Originally posted by dummyagain
If i want to write a C program in counting the no of file in a folder, what should i do?
It's pretty simple. Look at opendir(3), readdir(3), stat(3), and closedir(3). Here's an example:

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

int main()
   struct dirent*  dirEntry   = NULL;
   DIR*            dirStream  = NULL;
   int             fileCount  = 0;
   struct stat     fileStats;

   dirStream  = opendir( "." );

   while( ( dirEntry = readdir( dirStream ) ) != NULL ) {

      stat( dirEntry->d_name, &fileStats );

      if( !S_ISDIR( fileStats.st_mode ) ) {
         printf( "%s\n", dirEntry->d_name );


   printf( "Total number of files: %d\n", fileCount );

   closedir( dirStream );

Of course, you should check each function call to make sure it succeeds.
Old 09-24-2003, 02:28 AM   #4
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 33
This gives the number of files in directory....

#include <sys/stat.h>
int filter(const struct dirent *dir)
        struct stat BUFFER;
        return !S_ISDIR(BUFFER.st_mode);
        struct dirent **name_list;


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
counting statistics rafalek Linux - Networking 0 05-21-2004 03:39 PM
4 days and counting tomank Linux - Newbie 7 04-06-2004 10:22 AM
Three Years and Counting! trickykid General 9 01-06-2004 06:30 PM
And counting... MasterC General 18 09-24-2003 05:07 AM
counting tool stand Linux - Software 3 08-23-2003 12:54 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:21 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration