LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices



Reply
 
Search this Thread
Old 09-24-2003, 12:13 AM   #1
dummyagain
Member
 
Registered: Sep 2003
Posts: 74

Rep: Reputation: 15
counting


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, 01:09 AM   #2
Mohsen
Member
 
Registered: Feb 2003
Location: Iran
Distribution: Solaris 10
Posts: 201

Rep: Reputation: 30
Here is borrowed from Mr.Ritchie
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.

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

Rep: Reputation: 31
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.
 
Old 09-24-2003, 03:28 AM   #4
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 32
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));
}
 
  


Reply


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 04:39 PM
4 days and counting tomank Linux - Newbie 7 04-06-2004 11:22 AM
Three Years and Counting! trickykid General 9 01-06-2004 07:30 PM
And counting... MasterC General 18 09-24-2003 06:07 AM
counting tool stand Linux - Software 3 08-23-2003 01:54 AM


All times are GMT -5. The time now is 02:39 AM.

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