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"

C list folder files and only show regular file

 

C list folder files and only show regular file

 

上一篇:静态编译和动态编译学习-基于gcc/g++


下一篇:python代码tools——文字替换