Home
 
 
Search:  
C C++ Perl PHP Python HTML ShellScripts
 
 
  Coding Books
  Tutorials
  Search Code
  Browse Code
  Link to Us
  Site News
  Contact Metalshell
 
 
 
  Submit Code   Statistics
 



GCC: The Complete Reference    (ISBN: 0072224053)


 

 List Price: $59.99
 Our Price: $41.99
 Used Price:

 Release Date: 28 August, 2002
 Manufacturer: McGraw-Hill Osborne Media (Paperback)
 Sales Rank: 19,110

 Author: Arthur Griffith









More Info

 List Contents of a Directory 2003-01-19 20:35:42
 
Category: source:c:linux
Description: List sorted contents of a directory excluding '.' and '..'.
Platform: unix
Author: detour
Viewed: 4450
Rating: 4.2/5 (26 votes)
If you have any questions about this piece of code or still need help, try posting your question on the forum.

 

Printable Version
listdir.c
/* listfiles.c by detour@metalshell.com
 *
 * List sorted contents of a directory excluding '.' and
 * '..'.
 *
 * http://www.metalshell.com
 *
 */

#include <sys/dir.h>
#include <stdio.h>
#include <dirent.h>

int dir_filter(const struct direct *entry) {
  if ((strcmp(entry->d_name, ".") == 0) ||
      (strcmp(entry->d_name, "..") == 0))
    return 0;

return 1;
}

int dir_read(const char *dir) {
  struct direct **files;
  int nfiles, x;
  int dir_filter(const struct direct *entry);

  nfiles = scandir(dir, &files, dir_filter, alphasort);

  if(nfiles <= 0)
    return 0;

  for(x=0;x<nfiles;x++)
    fprintf(stdout, "File: %s\n", files[x]->d_name);

  return 1;
}

int main() {
  dir_read("/tmp");

  return 0;
}
Rate this code:
(Not Helpful)  (Very Helpful) 

 
 
   Developer.*  
   Blue Parrots  
   Technipal  
   Defy Magazine  
   Code Project  
   Prog. Heaven  


Got Money?