how to use scandir()?
George Turczynski
george at hls0.hls.oz
Fri Aug 31 16:32:17 AEST 1990
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?
> Eg., how to list the filenames of a given directory?
>
> It seems to me that
>
> struct direct *(*namelist[]); is a reasonable declaration,
try: struct direct **namelist;
> n = scandir(".", namelist, NULL, NULL) is a reasonable call,
try: n= scandir(".",&namelist,NULL,NULL);
> for (i=0; i<n; i++)
> printf("filename = %s\n", (*namelist[i])->d_name); is a reasonable use,
try: for( i= 0; i < n; i++ )
printf("filename= %s\n",namelist[i]->d_name);
> however, I'm getting Segmentation Faults all the time.
> Someone kindly points out what I did wrong?
>
> Thanks. Please send to newsgroup as my email is unstable.
Perhaps you couldn't make sense of the manual entry ? I assume you
_did_ read it, no ? You give it the address of a struct direct ** and
it malloc()s the space for the pointer table and the structures, then
"fills in" the struct direct ** ("namelist" here) with a pointer to
the pointer table. You then use the pointer table to get to the
struct directs.
Here is the example you asked for:
/*-----------------Cut here--------------------*/
#include<stdio.h>
#include<sys/types.h>
#include<sys/dir.h>
struct direct **namelist;
main()
{
int i, n;
n= scandir(".",&namelist,NULL,NULL);
for( i= 0; i < n; i++ )
printf("filename= %s\n",namelist[i]->d_name);
}
/*-----------------Cut here--------------------*/
Hope this clears up any problems,
and have a nice day...
--
| George P. J. Turczynski. |----------------------------------------------------
| Computer Systems Engineer. | ACSnet: george at highland.oz | I can't speak for the |
| Highland Logic Pty. Ltd. | Phone: +61 48 683490 | company, I can barely |
| Suite 1, 348-354 Argyle St | Fax: +61 48 683474 | speak for myself... |
| Moss Vale. NSW. Australia. 2577 |----------------------------------------------------
More information about the Comp.unix.questions
mailing list