zoukankan      html  css  js  c++  java
  • 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);
    }

  • 相关阅读:
    des加密
    http请求报错
    js生成二维码(jquery自带)
    tomcat跨域请求
    jsp读取properties文件
    spring+mybatis整合读取不了配置文件
    svn提交报e200007错误
    firefox兼容性问题
    Oracle学习笔记(2)
    Spring设置定时器:quartz
  • 原文地址:https://www.cnblogs.com/Fred1987/p/15618830.html
Copyright © 2011-2022 走看看