home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / c / 18857 < prev    next >
Encoding:
Text File  |  1992-12-24  |  1.4 KB  |  51 lines

  1. Path: sparky!uunet!usc!wupost!crcnis1.unl.edu!moe.ksu.ksu.edu!matt.ksu.ksu.edu!news
  2. From: probreak@matt.ksu.ksu.edu (James Michael Chacon)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Methods to find all files' name of the directory in UNIX
  5. Date: 24 Dec 1992 04:29:11 -0600
  6. Organization: Kansas State University
  7. Lines: 39
  8. Message-ID: <1hc3dnINNjcq@matt.ksu.ksu.edu>
  9. References: <1992Dec24.043515.15224@dec8.ncku.edu.tw>
  10. NNTP-Posting-Host: matt.ksu.ksu.edu
  11.  
  12. wangbs@casd1.iie.ncku writes:
  13.  
  14.  
  15. >   Hi, all:
  16. >       How can I program to find all files' name of a directory in UNIX?
  17. >   It is better having a example to show this.   HELP!!!
  18. >   
  19. >                Thanks for any response.
  20.  
  21. >                                        wangbs@casd1.iie.ncku.edu.tw
  22.  
  23. In the book Advanced Programming In The Unix Environment they give an 
  24. example using opendir. This then returns you a pointer to a DIR structure.
  25. You can run readdir on it until it returns null to get the name of each file
  26. in the directory. Below is the example they gave.
  27.  
  28. James
  29.  
  30.  
  31. -- Cut Here --
  32.  
  33. #include <sys/types.h>
  34. #include <dirent.h>
  35. #include <ourhdr.h>
  36.  
  37. int
  38. main(int argc, char *argv[])
  39. {
  40.   DIR *dp;
  41.   struct dirent *dirp;
  42.   if (argc != 2)
  43.     err_quit("a single argument (the directory name) is required");
  44.   if ((dp = opendir(argv[1])) == NULL)
  45.     err_sys("can't open %s", argv[1]);
  46.   while ((dirp = readdir(dp)) != NULL)
  47.     printf("%s\n", dirp->d_name);
  48.   closedir(dp);
  49.   exit(0);
  50. }
  51.