美文网首页
C语言常用代码片断

C语言常用代码片断

作者: louyang | 来源:发表于2017-12-08 18:39 被阅读11次
    • 读目录
    读目录
    #include <stdio.h>  //perror()
    #include <stdlib.h> //exit()
    #include <dirent.h> //struct dirent,DIR,opendir(),readdir()
    #include <string.h> //strcmp()
    
    void die(char * s)
    {
        perror(s);
        exit(1);
    }
    
    int main()
    {
        struct dirent * entry;
        DIR * dir;
        int id;
    
        dir = opendir("/var/run/netns");
        if (!dir)
        {
            die("opendir");
        }
    
        while ((entry = readdir(dir)))
        {
            if (strcmp(entry->d_name, ".") == 0)
                continue;
            if (strcmp(entry->d_name, "..") == 0)
                continue;
            printf("%s\n", entry->d_name);
        }
    
        closedir(dir);
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:C语言常用代码片断

          本文链接:https://www.haomeiwen.com/subject/cqhyixtx.html