for linux os
The code below shall list all files and sub-directories in dir_path (for current directory use dir_path = ".").
DESCRIPTION
This description qouted from man7.org link
struct dirent *readdir(DIR *dirp);
readdir()
The readdir() function returns a pointer to a dirent structure
representing the next directory entry in the directory stream pointed
to by dirp. It returns NULL on reaching the end of the directory
stream or if an error occurred. for further details go to the link above for man7.org.
#include <stdio.h>
#include <dirent.h>
int main(void)
{
/* de is Pointer for directory entry */
struct dirent *de;
const char* dir_path = "C:/Users/me/Documents/Example";
/*opendir() returns a pointer of DIR type.*/
DIR *dr = opendir(dir_path);
if (dr == NULL) /* opendir returns NULL if couldn't open directory */
{
printf("Could not open current directory" );
return 0;
}
while ((de = readdir(dr)) != NULL){
printf("%s\n", de->d_name);
}
closedir(dr);
return 0;
}
for windows os
for windows use the header file: fileapi.h see microsoft docs here: fileapi.h
this question answered before in SO in the link below using the FindFirstFile, FindNextFile and FindClose functions.
please review the answer in the link: list directory in windows in C programming language