5

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.

asked Oct 24, 2010 at 17:02
3
  • 2
    This question has nothing to do with C or C++ but with the OS that you are using. Please be more precise. Commented 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++. Commented Oct 24, 2010 at 17:13
  • 3
    C 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. Commented Oct 24, 2010 at 18:27

5 Answers 5

4

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.

answered Oct 24, 2010 at 18:09
Sign up to request clarification or add additional context in comments.

1 Comment

For anyone searching, the page, though dark now is available on the wayback machine. web.archive.org/web/20190717230650/http://…
4

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;
}
answered Nov 3, 2010 at 23:20

Comments

2

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

answered Oct 24, 2010 at 17:08

Comments

1

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.

answered Dec 24, 2013 at 17:11

Comments

0

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.

answered Oct 24, 2010 at 20:48

4 Comments

Why was this down-voted? The answer is in this chapter in this book. I guess it was down-voted because you have to own a copy first? amazon.co.uk/C-Programming-Language-2nd/dp/0131103628 It's seriously worth buying if you're learning C.
@GaryWilloughby: This skirts very close to where K&R 2 shows its age. There's a function 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.)
I said "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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.