Listing files in a folder using C

I have a folder path like

/my folder

or on Windows:

C: \ my_folder

and I want to get a list of all the files in this folder. How to do it in C?

Difference in C ++ or C99?

How can I get a list of its folders?

Any help is appreciated.

+5
source share
5 answers

POSIX opendir() readdir(). Windows _findfirst() _findnext(). opendir() readdir() - Windows, API . .

+4

, dirent.h

dirent.h - C POSIX C , . C , "" .
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;
}
+4
+3

get_all_files_within_folder(), C/++ , , . . , .

+1
source

This is a classic problem, one possible solution can be found in Kernigan and Ritchie, the C programming language (chapter 8.6). The essence of the task is the recursive traffic of the target folder and its subfolders.

0
source

All Articles