Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting list of files in a directory 2

Status
Not open for further replies.

CaKiwi

Programmer
Apr 8, 2001
1,294
US
I am using gcc on mandrake linux and I need to get a list of files in a diectory. I have tried opening the directory but when I try to read it I get error 21 "Is a directory".
Any help greatly appreciated. CaKiwi
 
Hi, Try the following program.

#include <dirent.h>
#include <stdio.h>
int main()
{
struct dirent *s;
DIR *p;
if( (p = opendir(&quot;test&quot;)) == NULL)
{
printf(&quot;The Dir not found &quot;);
exit(-1);
}
while( (s = readdir(p)) != NULL)
{
printf(&quot;\n%s&quot;, s->d_name);
}
return 0;
}

Here I assumed &quot;test&quot; as directory to be explored.

Regards,
Maniraja S
 
Thanks smaniraja, I'll try it tonight. CaKiwi
 
One more thing, how do I get the file size? CaKiwi
 
I figured this one out. It is the st_size field of the stat structure returned by the stat() function. Shame I can't give myself a star. CaKiwi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top