zoukankan      html  css  js  c++  java
  • FTS(3) 遍历文件夹实例

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<sys/types.h>
    #include<sys/stat.h>
    #include<dirent.h>
    #include<fts.h>

    #define MAX 300

    struct file{

    char* file_name;
    long long file_size;
    };
    struct file files_in_dir[MAX];

    void travDir(char* dir_s, struct file filesindir[]); //遍历数组
     

    // 要求1个文件夹路径参数,用来作为遍历的起始点
    int main(int argc, char* argv[]){
    if(argc < 2){

    printf("Need a dir path to start.\n");
    exit(1);
    }

    travDir(argv[1], files_in_dir);

    printf("successfull\n");

    return 0;

    }

    void travDir(char* dir_s, struct file filesindir[]){

    //遍历文件夹,把文件夹里的mp4文件的文件信息存入struct

    FTS* fts;
    char** ptr_path;
    FTSENT* ftsfile;
    ptr_path = (char**)malloc(sizeof(char**));
    *ptr_path = dir_s;
    puts(*ptr_path);

    if( (fts = fts_open(ptr_path, FTS_PHYSICAL, NULL)) == NULL){
    printf("Can't open the dir %s. Can't get ftsent struct \n", dir_s);
    }
    int i = 0;
    while( (ftsfile = fts_read(fts)) != NULL){
    //开始遍历文件,获取文件信息
    // puts(ftsfile -> fts_name);
    char* filename = ftsfile -> fts_name;
    if(strstr(filename, ".mp4") != NULL){
    puts(filename);
    filesindir[i].file_name = filename;
    filesindir[i].file_size = ftsfile -> fts_statp -> st_size;
    printf(" the size of file == %lld\n", filesindir[i].file_size);
    i++;
    }
    }
    }

  • 相关阅读:
    需要union
    with语法,需要递归的面试题目
    聚合主分类,子查询获得子分类
    泛型
    RepeaterInMVC
    需要自己创建集合的题目
    Ollydbg入门
    svn服务器架设
    http与svn架设服务器
    svn错误信息一览表
  • 原文地址:https://www.cnblogs.com/patientAndPersist/p/3076436.html
Copyright © 2011-2022 走看看