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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

List all file in directory 2

Status
Not open for further replies.

DCCoolBreeze

Programmer
Jul 25, 2001
208
US
I would like to develop a routine that will loop through all files within a specific directory and write these filenames to an output file.
 
I think the easiest approach would be to execute a "ls" with its standard output pointing to a file. Try "man exec" on your shell if you're interested.
 
there exist one similar quiestion in this forum with an answer. John Fill
1c.bmp


ivfmd@mail.md
 
Hi,

Use the following program.

#include <dirent.h>
#include <stdio.h>
int main()
{
struct dirent *s;
DIR *p;
FILE *fp;
if( (p = opendir(&quot;test&quot;)) == NULL)
{
printf(&quot;The Dir not found &quot;);
exit(-1);
}
if( (fp = fopen(&quot;result.txt&quot;, &quot;w&quot;)) == NULL)
{
printf(&quot;Unable to open the result file&quot;);
exit(-1);
}
while( (s = readdir(p)) != NULL)
{
printf(&quot;\n%s&quot;, s->d_name);
fprintf(fp, &quot;\n%s&quot;, s->d_name);
}
return 0;
}


Maniraja S
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top