Pelles C forum

C language => Beginner questions => Topic started by: Gary Willoughby on February 17, 2010, 12:21:17 AM

Title: Whats the best way to walk a directory to list files?
Post by: Gary Willoughby on February 17, 2010, 12:21:17 AM
Whats the best way to walk a directory to list files?

Basically, i have a starting directory and i want to grab the files from that directory and display them in a console. Do i need to use the os api commands for that? Is there a cross-platform way?

Any ideas or snippets?
Title: Re: Whats the best way to walk a directory to list files?
Post by: TimoVJL on February 17, 2010, 08:03:32 AM
opendir() readdir() closedir() are usable in linux too.
Remember to use compiler option Define compatibility names.

_findfirst(), _findnext() and _findclose() are MSVC-functions for that.

Code: [Select]
/* opendir example*/
#include <stdio.h>
#include <dirent.h>

int main(int argc, char **argv)
{
DIR *dir;
struct dirent *de;

dir = opendir(".");
while(dir) {
de = readdir(dir);
if (!de) break;
printf("%i %s\n", de->d_type, de->d_name);
}
closedir(dir);
return 0;
}
Title: Re: Whats the best way to walk a directory to list files?
Post by: Gary Willoughby on February 20, 2010, 06:11:00 PM
Thanks a lot!

I had to change it to this to work in PellesC

Code: [Select]
#include <dirent.h>

int main(int argc, char **argv)
{
_DIR *dir;
struct _dirent *de;

dir = _opendir(".");
while(dir) {
de = _readdir(dir);
if (!de) break;
printf("%i %s\n", de->d_type, de->d_name);
}
_closedir(dir);
return 0;
}
Title: Re: Whats the best way to walk a directory to list files?
Post by: TimoVJL on February 20, 2010, 06:31:08 PM
Remember to use compiler option Define compatibility names.
So use -Go  -> (oldnames.lib)
Title: Re: Whats the best way to walk a directory to list files?
Post by: Gary Willoughby on February 20, 2010, 11:07:29 PM
Ah right. What does that flag do?
Title: Re: Whats the best way to walk a directory to list files?
Post by: Stefan Pendl on February 20, 2010, 11:20:53 PM
What does that flag do?

Did you already check the Pelles C documentation of the compiler and linker options?
Title: Re: Whats the best way to walk a directory to list files?
Post by: Gary Willoughby on February 21, 2010, 12:19:49 AM
What does that flag do?

Did you already check the Pelles C documentation of the compiler and linker options?

Got it thanks! :)