how to use scandir()?
WATANABE Katsuhiro
katsu at sra.co.jp
Tue Aug 28 11:54:57 AEST 1990
# Sorry for my poor English.
In article <9008271747.AA02292 at ws-38.cae.wisc.edu> wyuen at CAE.WISC.EDU writes:
> Could someone give examples of how to use scandir?
> struct direct *(*namelist[]); is a reasonable declaration,
> n = scandir(".", namelist, NULL, NULL) is a reasonable call,
It's a `illegal pointer probrem'. The 2nd argument of scandir() must
be the address in which you want scandir() to set the address of name list.
Indeed TYPE of each identifiers are surely reasonable in your program.
But `namelist' is uninitialized and it holds nonsense address and its VALUE
is not reasonable.
Anyway, my example:
#include <sys/types.h>
#include <sys/dir.h>
main(argc, argv)
int argc;
char **argv;
{
int i, n;
struct direct **namelist;
if ((n = scandir(".", &namelist, NULL, NULL)) < 0) {
perror(argv[0]);
exit(1);
}
for (i = 0; i < n; i++)
printf("filename = %s\n", namelist[i]->d_name);
/* example of free-ing */
for (i = 0; i < n; i++) {
free(namelist[i]->d_name);
}
free(namelist);
exit(0);
}
----____----____
WATANABE Katsuhiro Software Research Associates, Inc. Japan.
Not execute, but evaluate.
More information about the Comp.unix.questions
mailing list