How to traverse all files in the directory using C language

How to traverse all files in the directory using C language

How to view all files in the current directory

/*
Example: display the names of all files in the current directory using C language
*/

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>

int main(int argc, char const *argv[]) {
    DIR *d;
    struct dirent *dir;
    d = opendir(".");
    if (d) {
        while ((dir = readdir(d)) != NULL) {
            printf("%s\n", dir->d_name);
        }
        closedir(d);
    }
    return(0);
}
S‮o‬urce:www.lautturi.com

example of output:

.
..
bin
hello.cbp
hello.cscope_file_list
hello.depend
hello.layout
main.c
obj
test.txt
Created Time:2017-08-29 04:37:41  Author:lautturi