home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!usc!wupost!crcnis1.unl.edu!moe.ksu.ksu.edu!matt.ksu.ksu.edu!news
- From: probreak@matt.ksu.ksu.edu (James Michael Chacon)
- Newsgroups: comp.lang.c
- Subject: Re: Methods to find all files' name of the directory in UNIX
- Date: 24 Dec 1992 04:29:11 -0600
- Organization: Kansas State University
- Lines: 39
- Message-ID: <1hc3dnINNjcq@matt.ksu.ksu.edu>
- References: <1992Dec24.043515.15224@dec8.ncku.edu.tw>
- NNTP-Posting-Host: matt.ksu.ksu.edu
-
- wangbs@casd1.iie.ncku writes:
-
-
- > Hi, all:
- > How can I program to find all files' name of a directory in UNIX?
- > It is better having a example to show this. HELP!!!
- >
- > Thanks for any response.
-
- > wangbs@casd1.iie.ncku.edu.tw
-
- In the book Advanced Programming In The Unix Environment they give an
- example using opendir. This then returns you a pointer to a DIR structure.
- You can run readdir on it until it returns null to get the name of each file
- in the directory. Below is the example they gave.
-
- James
-
-
- -- Cut Here --
-
- #include <sys/types.h>
- #include <dirent.h>
- #include <ourhdr.h>
-
- int
- main(int argc, char *argv[])
- {
- DIR *dp;
- struct dirent *dirp;
- if (argc != 2)
- err_quit("a single argument (the directory name) is required");
- if ((dp = opendir(argv[1])) == NULL)
- err_sys("can't open %s", argv[1]);
- while ((dirp = readdir(dp)) != NULL)
- printf("%s\n", dirp->d_name);
- closedir(dp);
- exit(0);
- }
-