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.
- 
 2This question has nothing to do with C or C++ but with the OS that you are using. Please be more precise.Jens Gustedt– Jens Gustedt2010年10月24日 17:09:35 +00:00Commented Oct 24, 2010 at 17:09
- 
 @Jens Uh? Where do you see the ambiguity? Plus it has a lot to do with C or C++.Šimon Tóth– Šimon Tóth2010年10月24日 17:13:58 +00:00Commented Oct 24, 2010 at 17:13
- 
 3C as a language has IO on files, but doesn't even know the concept of directories. Directories are an OS concept, so to say anything useful to answer that question you'd have to know the OS.Jens Gustedt– Jens Gustedt2010年10月24日 18:27:21 +00:00Commented Oct 24, 2010 at 18:27
5 Answers 5
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.
1 Comment
You can use the functions declared in dirent.h
dirent.h is the header in the C POSIX library for the C programming language that contains constructs that facilitate directory traversing. The function is not part of the C standard, but is considered "pseudo-standard" and is usually portable between platforms.
http://en.wikipedia.org/wiki/Dirent.h
#include <dirent.h>
int main(int argc, char **argv)
{
 DIR *dir;
 struct dirent *de;
 dir = opendir("."); /*your directory*/
 while(dir)
 {
 de = readdir(dir);
 if (!de) break;
 printf("%i %s\n", de->d_type, de->d_name);
 }
 closedir(dir);
 return 0;
}
Comments
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
Comments
Check out the get_all_files_within_folder() I wrote in C/C++ here, which I answered a similar question as yours. It works perfectly for me. Hope it helps.
Comments
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.
4 Comments
dirwalk() in the chapter that is usable. Almost immediately afterwards, there's a sample implementation of opendir(), readdir(), closedir() which is no use on any current Unix system — but which used to work back in the 80s and early 90s. (I'm not sure exactly when it stopped working; it depends on platform, in part, but most POSIX-ish O/S had moved to "you must use our opendir() et al and cannot use the one from K&R 2 any more" by, say, 2005, and probably quite a lot earlier.)dirwalk() is usable". It requires some minor tweaks to make it usable with POSIX standard opendir() et al. The type Dirent needs to become struct dirent (or you need to add typedef struct dirent Dirent;), and references to dp->name need to be changed to dp->d_name.