Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!samsung!munnari.oz.au!metro!wolfen!hls0!george From: george@hls0.hls.oz (George Turczynski) Newsgroups: comp.unix.questions Subject: Re: how to use scandir()? Summary: Not reasonable... Message-ID: <857@hls0.hls.oz> Date: 31 Aug 90 06:32:17 GMT References: <9008271747.AA02292@ws-38.cae.wisc.edu> Lines: 64 In article <9008271747.AA02292@ws-38.cae.wisc.edu>, wyuen@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 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 #include #include 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@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 |----------------------------------------------------