zoukankan      html  css  js  c++  java
  • linux c遍历文件夹 和文件查找的方法

    linux c遍历文件夹的方法比较简单,使用c来实现

    #include <iostream>
    #include <stdio.h>
    
    #include <sys/types.h>
    #include <dirent.h>
    #include <sys/dir.h>
    #include <sys/stat.h>
    
    ...
    enum
    {
        DT_UNKNOWN = 0,     //未知类型
        DT_FIFO = 1,        //管道
        DT_CHR = 2,         //字符设备文件
        DT_DIR = 4,         //目录
        DT_BLK = 6,         //块设备文件
        DT_REG = 8,         //普通文件
        DT_LNK = 10,        //连接文件
        DT_SOCK = 12,       //套接字类型
        DT_WHT = 14         //
    };
    void loopDir(const char *dir_path) { 
      char temp[256] = {0};
      struct dirent *pdirent;
    DIR
    *d_info = opendir(dir_path); if (d_info) { while ((pdirent = readdir(d_info)) != NULL) { if (pdirent->d_type & DT_REG) { //普通文件 } if (pdirent->d_type & DT_DIR) { //目录,递归 } } } } ...

    在类Unix或linux (OSX,iOS,Android系统)下面搜索某一类型的文件可以用OgreSearchOps来使用,非常方便,对应 win32 则需要另外一套机制

    // 头文件引用部分
    #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
        #include <io.h>
    #else
        #include <unistd.h>
        #include <stdio.h>
        #include <dirent.h>
        #include <sys/stat.h>
    
        #include "OgreSearchOps.h"
    #endif
    
    
    void MfindFiles(const std::string &dir_path,const std::string &partten,std::vector<std::string> &arr)
    {
    #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
        _finddata_t FileInfo;
        string strfind = dir_path + "\" + partten;
        long Handle = _findfirst(strfind.c_str(), &FileInfo);
        
        if (Handle == -1L)
        {
            exit(-1);
        }
        do{
            if (FileInfo.attrib & _A_SUBDIR)
            {
            }
            else
            {
                string filename = (dir_path + "\" + FileInfo.name);
                arr.push_back(FileInfo.name);
            }
        }while (_findnext(Handle, &FileInfo) == 0);
        
        
        _findclose(Handle);
    #else
        findFiles(dir_path, partten, false, false, &arr);
    #endif
    }
    
    // 测试  搜索所有的.plist 后缀名文件
        std::vector<std::string> plist_file_list;
        MfindFiles(effect_path, "*.plist",plist_file_list);
    
  • 相关阅读:
    PL/SQL跨库查询数据
    oracle 两个时间相减
    导出Excel格式数据
    Java导出pdf文件数据
    $.ajax相关用法
    oracle 删除掉重复数据只保留一条
    常用Oracle操作语句
    JS请求服务器,并返回信息,请求过程中不需要跳转页面
    tomcat部署web项目的3中方法
    Date()日期转换和简单计算
  • 原文地址:https://www.cnblogs.com/howeho/p/4213712.html
Copyright © 2011-2022 走看看