tags:

views:

132

answers:

4

Hi

I have path to a folder for example

/myfolder

or in Windows:

C:\myfolder

and I want to get a list of all files in that folder. How shall I do so in C?

Is it different in C++ or C99?

How can I get a list of its folders?

Any help is appreciated.

+3  A: 

The best approach in C++ is using boost filesystem.

As for C, you will need platform API (POSIX/WinAPI).

POSIX documentation + example: http://www.opengroup.org/onlinepubs/009695399/functions/readdir.html

Let_Me_Be
+1  A: 

In POSIX operating systems, you can call opendir() and readdir(). In Windows you can call _findfirst() and _findnext(). With a little effort you can implement your own opendir() and readdir() as wrapper functions under Windows, so that your application code can use the same API everywhere. An example of that can be found here.

Jeremy Friesner
A: 

This is classical task, one possiple solution maybe find in Kernigan & Ritchie - The C programming Language (Chapter 8.6). Essence of task is recursive traverse of target folder and its subfolders.

StNickolay