C list folder files and only show regular file
#include <stdio.h> #include <stdlib.h> #include <uuid/uuid.h> #include <string.h> #include <dirent.h> #include <limits.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> int isRegularFile(const char *path) { struct stat pathStat; stat(path, &pathStat); return S_ISREG(pathStat.st_mode); } int main() { listDirFiles8(); return 0; } void listDirFiles8() { char fullName[PATH_MAX]; DIR *dirStruct; struct dirent *dir; dirStruct = opendir("."); while ((dir = readdir(dirStruct))) { if (isRegularFile(dir->d_name)) { realpath(dir->d_name, fullName); printf("%s ", dir->d_name); } } printf("\n"); }
Compile and run as below
gcc -g h2.c -o h2 -luuid
./h2
The final effect as the linux command "ls"
void listDirFiles8() { char fullName[PATH_MAX]; DIR *dirStruct; struct dirent *dir; dirStruct = opendir("."); while ((dir = readdir(dirStruct))) { if (isRegularFile(dir->d_name)) { realpath(dir->d_name, fullName); printf("%s ", dir->d_name); } } printf("\n"); } int isRegularFile(const char *path) { struct stat pathStat; stat(path, &pathStat); return S_ISREG(pathStat.st_mode); }