zoukankan      html  css  js  c++  java
  • opendir 与 readdir

    https://blog.csdn.net/q278233370/article/details/99681142

    输入一个目录,输出目录下面所有文件的大小时间戳

    #include <unistd.h>
    #include <stdio.h>
    #include <errno.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <dirent.h>
    #include <time.h>
    
    int get_file_size_time(const char *filename) {
        struct stat statbuf;
        if (stat(filename, &statbuf) == -1) {
            printf("Get stat on %s Error:%d
    ",filename, strerror(errno));
            return -1;
        }
        if (S_ISDIR(statbuf.st_mode)) {
            printf("%s is a dir 
    ",filename);
            return 0;
        }
        if (S_ISREG(statbuf.st_mode)) {
            printf("%s size:%ld bytes	modified at %s",filename, statbuf.st_size, ctime(&statbuf.st_mtime));
            return 0;
        }
    }
    
    int main(int argc, char **argv) {
        DIR *dirp;
        struct dirent *direntp;
        char buf[80];
        if (argc != 2) {
            printf("Usage:%s filename
    a", argv[0]);
            return -1;
        }
        if (-1 == get_file_size_time(argv[1]))
            return 0;
        if ((dirp = opendir(argv[1])) == NULL) {
            printf("Open Directory %s Error:%d
    ", argv[1], strerror(errno));
            return -1;
        }
        while ((direntp = readdir(dirp))){
            sprintf(buf,"%s/%s",argv[1],direntp->d_name);
            printf("filename=%s
    ",direntp->d_name);
            get_file_size_time(buf);
        }
        closedir(dirp);
        return 0;
    }

    在congfig下面的目录中查找一个.txt文件

    #include <stdio.h>
    #include <dirent.h>
    #include <iostream>
    #include <string>
    #include <string.h>
    
    using namespace std;
    
    int GetProject(std::string* psProjectPath)
    {
        int ret = -1;
        DIR *dir;
        struct dirent *ptr;
        if (dir=opendir("config"))
        {
            ret = 1;
            while ((ptr=readdir(dir)) != NULL)
            {
                if(ptr->d_type != DT_DIR) continue;
                if((strcmp(ptr->d_name,".") == 0) || (strcmp(ptr->d_name,"..") == 0)) continue;
                DIR *dir2;
                struct dirent *ptr2;
                char path[128];
                sprintf(path, "config/%s", ptr->d_name);
                cout << "path = " << ptr->d_name << endl;
                if (dir2=opendir(path))
                {
                    while ((ptr2=readdir(dir2)) != NULL)
                    {
                        if(ptr2->d_type != DT_REG) continue;
                        if(!strstr(ptr2->d_name, ".txt")) continue;
                        sprintf(path, "config/%s/%s", ptr->d_name, ptr2->d_name);
                        *psProjectPath = path;
                        ret = 0;
                        break;
                    }
                    closedir(dir2);
                }
           //    break;
            }
            closedir(dir);
        }
            return ret;
    }
    int main()
    {
            string path;
    
            int ret = GetProject(&path);
    
            cout << "path = " << path << endl;
    
    }
  • 相关阅读:
    hdu 1704 Rank(floyd传递闭包)
    codeforces 85D. Sum of Medians(线段树or分块)
    hdu 1706 The diameter of graph(folyd计数)
    hdu 4705 Y(树形DP)
    hdu 4123 Bob’s Race(树形DP+rmq)
    hdu 2196 Computer(树形DP)
    hdu 3534 Tree(树形DP)
    hdu 6017 Girls Love 233(dp)
    [HDU5903]Square Distance(DP)
    HDU5878~HDU5891 2016网络赛青岛
  • 原文地址:https://www.cnblogs.com/nanqiang/p/13468596.html
Copyright © 2011-2022 走看看