[comp.lang.c] all filenames in directory in array/list

mstuurm@cs.vu.nl (Mark Stuurman) (02/06/90)

Could someone tell me how read all filenames in a directory into
an array or a list (of filenames of course).

                             Many thanks in advance,
                             Mark Stuurman (mstuurm@cs.vu.nl)

P.S. We use SunOS 4.0

henry@utzoo.uucp (Henry Spencer) (02/07/90)

In article <5302@star.cs.vu.nl> mstuurm@cs.vu.nl (Mark Stuurman) writes:
>Could someone tell me how read all filenames in a directory into
>an array or a list (of filenames of course)...
>P.S. We use SunOS 4.0

First step is to read the directory(3) manual page.  Actually, unless you
have trouble understanding it, that's all you need.
-- 
SVR4:  every feature you ever |     Henry Spencer at U of Toronto Zoology
wanted, and plenty you didn't.| uunet!attcan!utzoo!henry henry@zoo.toronto.edu

geer-john@CS.YALE.EDU (John Yuson Geer) (02/07/90)

In article <5302@star.cs.vu.nl> mstuurm@cs.vu.nl (Mark Stuurman) writes:
>Could someone tell me how read all filenames in a directory into
>an array or a list (of filenames of course).
>

	"scandir" is what you seek.  It even allocates the array
	of directory structures!

#include<stdio.h>
#include <sys/types.h>
#include <sys/dir.h>
main()
{
	int num,i;
	struct direct **namelist;
	char dir[] = "/usr/bin/X11";
	num = scandir(dir,&namelist,NULL,NULL);

	for(i=0;i<num;i++)
		fprintf(stderr,"%s\n",namelist[i]->d_name);
}

	
>                             Many thanks in advance,
>                             Mark Stuurman (mstuurm@cs.vu.nl)
>
>P.S. We use SunOS 4.0

no prob!

jyg'
---         
geer@CS.YALE.EDU	"This was because he was no mere dreamer but one
readnews be news	 one of those dreamer-doers, a guy with a program."
				-_Henderson_the_Rain_King_  by Saul Bellow
Hochspannung Lebensgefahr