LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   counting (https://www.linuxquestions.org/questions/programming-9/counting-96161/)

dummyagain 09-23-2003 11:13 PM

counting
 
If i want to write a C program in counting the no of file in a folder, what should i do?

Mohsen 09-24-2003 12:09 AM

Here is borrowed from Mr.Ritchie :D
Code:


#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).
Code:

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)
  continue;
  printf ("file: %s\n", dp->name);
}


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

eric.r.turner 09-24-2003 12:59 AM

Re: counting
 
Quote:

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:

Code:

#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 );
        fileCount++;
      }

  }

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

  closedir( dirStream );

  return(0);
}

Of course, you should check each function call to make sure it succeeds.

SaTaN 09-24-2003 02:28 AM

This gives the number of files in directory....

Code:

#include<dirent.h>
#include <sys/stat.h>
int filter(const struct dirent *dir)
{
        struct stat BUFFER;
        stat(dir->d_name,&BUFFER);
        return !S_ISDIR(BUFFER.st_mode);
}
main()
{
        struct dirent **name_list;
        printf("%d",scandir(".",&name_list,filter,alphasort));
}



All times are GMT -5. The time now is 12:51 PM.