Whats the best way to walk a directory to list files?

Started by Gary Willoughby, February 17, 2010, 12:21:17 AM

Previous topic - Next topic

Gary Willoughby

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?

TimoVJL

#1
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.

/* 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;
}
May the source be with you

Gary Willoughby

Thanks a lot!

I had to change it to this to work in PellesC


#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;
}

TimoVJL

Remember to use compiler option Define compatibility names.
So use -Go  -> (oldnames.lib)
May the source be with you

Gary Willoughby


Stefan Pendl

Quote from: Gary Willoughby on February 20, 2010, 11:07:29 PM
What does that flag do?

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

Proud member of the UltraDefrag Development Team

Gary Willoughby