[comp.unix.programmer] Need a little example of scandir

stein@WRC.XEROX.COM (Adam Stein) (05/22/91)

I have been trying to use scandir() with little success.  I have included below
a piece of code trying to use scandir.  The code calls scandir() to get
all the files in a directory and then goes to print out the name of each
file.  The problem is that the namelist given to scandir() comes back empty.
I can't get to the filenames.  I'm using scandir() because it's easier
for scandir() to put together a list of files and I plan on using
the select and compare functions that scandir() would call.  If anybody knows
what I'm doing wrong or has a simple example that works, please let me know.
Thanx.

						Adam Stein
						stein.wbst129@xerox.com

-----Example----------Example----------Example----------Example---------
#include <sys/types.h>
#include <sys/dir.h>

myroutine()
{
	int numfiles,loop;
	struct direct *namelist;

	if((numfiles = scandir(".",&namelist,NULL,NULL)) == -1) {
	  perror("myprogram");
	  exit(1);
	}

	for(loop = 0;loop < numfiles;++loop)
	  printf("Name -> <%s>\n",namelist[loop].d_name);
}
--
Adam Stein @ Xerox Corporation         Email: stein.wbst129@xerox.com
                                              stein@arisia.xerox.com
Disclaimer: Any/All views expressed           ...!uunet!xerox.com!stein.wbst129
here have been proved to be my own.    Hopefully one of the above will work.

subbarao@phoenix.Princeton.EDU (Kartik Subbarao) (05/22/91)

>#include <sys/types.h>
>#include <sys/dir.h>
>
>myroutine()
>{
>	int numfiles,loop;
>	struct direct *namelist; 

should be struct direct **namelist; 

>
>	if ((numfiles = scandir(".", namelist, NULL, NULL)) == -1) {

should be scandir(".", &namelist, NULL, NULL);
...
etc

>	  perror("myprogram");
>	  exit(1);
>	}
>
>	for(loop = 0; loop < numfiles; ++loop)
>	  printf("Name -> <%s>\n",namelist[loop].d_name);

should be namelist[loop]->d_name

>}


As the man page says. It's a weird man page.


		-Kartik


--
internet% ypwhich

subbarao@phoenix.Princeton.EDU -| Internet
kartik@silvertone.Princeton.EDU (NeXT mail)  
SUBBARAO@PUCC.BITNET			          - Bitnet

jik@athena.mit.edu (Jonathan I. Kamens) (05/22/91)

In article <822@rocksanne.WRC.XEROX.COM>, stein@WRC.XEROX.COM (Adam Stein) writes:
|> I have been trying to use scandir() with little success.
|> ...
|> 
|> -----Example----------Example----------Example----------Example---------
|> ...
|> 	struct direct *namelist;

  Here's the problem.  Scandir does not create an array of struct direct, it
creates an array of struct direct *.  Therefore, this declaration should read

	struct direct **namelist;

|> ...
|> 	  printf("Name -> <%s>\n",namelist[loop].d_name);

  Since namelist is now an array of pointers instead of an array of
structures, you need to use -> indirection to get at its contents.  This line
should read

	  printf("Name -> <%s>\n",namelist[loop]->d_name);

-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik@Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710