How to display the names of all files in the current directory in Linux with C

1 Answer

0 votes
#include <dirent.h> 
#include <stdio.h>

int main(void)
{
    DIR *d;
    struct dirent *dir;
    
    d = opendir(".");
    if (d) {
        while ((dir = readdir(d)) != NULL) {
            printf("%s\n", dir->d_name);
        }
    }
    closedir(d);
    
    return 0;
}



/*
run:

.
..
.bash_logout
.bashrc
.profile

*/

 



answered Jan 26, 2025 by avibootz
...