清泛IT社区

搜索
App Inventor 2 中文社区 轻松创建Apps

扫码访问移动社区 移动社区,您的掌上技术专家

关注我,精彩不错过! 关注我,精彩不错过!

扫码安装最新版AI伴侣 最新版AI伴侣v2.69

Aia Store .aia 源码一站式解决方案 发布日志AI2连接测试ai2Starter模拟器

开通VIP免费赠送积分App Inventor 2 拓展有奖征文 VIP会员享专有教程,免费赠送基础版*技术支持服务! AI2入门必读中文文档中文教程IoT专题

查看: 432|回复: 0
打印 上一主题 下一主题

VC/Linux C++ 递归访问目录下所有文件

  • TA的每日心情
    开心
    2024-02-17 18:16
  • 签到天数: 14 天

    [LV.3]偶尔看看II

    546

    主题

    715

    帖子

    1万

    积分

    管理员

    这里没有广告...

    Rank: 9Rank: 9Rank: 9

    积分
    10689
    QQ
    跳转到指定楼层
    楼主
    发表于 2016-02-29 15:23:25 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    VC函数,部分代码如下:
    1. find(char * lpPath)
    2. {
    3.     char szFind[MAX_PATH];
    4.     WIN32_FIND_DATA FindFileData;

    5.     strcpy(szFind,lpPath);
    6.     strcat(szFind,"\\*.*");

    7.     HANDLE hFind=::FindFirstFile(szFind,&FindFileData);
    8.     if(INVALID_HANDLE_VALUE == hFind)    return;
    9.    
    10.     while(TRUE)
    11.     {
    12.         if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    13.         {
    14.             if(FindFileData.cFileName[0]!='.')
    15.             {
    16.                 strcpy(szFile,lpPath);
    17.                 strcat(szFile,"\");
    18.                 strcat(szFile,FindFileData.cFileName);
    19.                 find(szFile);
    20.             }
    21.         }
    22.         else
    23.         {
    24.             cout << FindFileData.cFileName;
    25.         }
    26.         if(!FindNextFile(hFind,&FindFileData))    break;
    27.     }
    28.     FindClose(hFind);
    29. }
    复制代码

    Linux C++实例如下:
    1. #include <dirent.h>
    2. #include <iostream>
    3. #include <cstdlib>
    4. #include <cstring>
    5. using namespace std;
    6. void GetFileInDir(string dirName)
    7. {
    8.     DIR* Dir = NULL;
    9.     struct dirent* file = NULL;
    10.     if (dirName[dirName.size()-1] != '/')
    11.     {
    12.         dirName += "/";
    13.     }
    14.     if ((Dir = opendir(dirName.c_str())) == NULL)
    15.     {
    16.         cerr << "Can't open Directory" << endl;
    17.         exit(1);
    18.     }
    19.     while (file = readdir(Dir))
    20.     {
    21.         //if the file is a normal file
    22.         if (file->d_type == DT_REG)
    23.         {
    24.             cout << dirName + file->d_name << endl;
    25.         }
    26.         //if the file is a directory
    27.         else if (file->d_type == DT_DIR && strcmp(file->d_name, ".") != 0 && strcmp(file->d_name, "..") != 0)
    28.         {
    29.             GetFileInDir(dirName + file->d_name);
    30.         }
    31.     }
    32. }
    33. int main(int argc, char* argv[])
    34. {
    35.     if (argc < 2)
    36.     {
    37.         cerr << "Need Directory" << endl;
    38.         exit(1);
    39.     }
    40.     string dir = argv[1];
    41.     GetFileInDir(dir);
    42. }
    复制代码

    清泛网 - 专注IT技能提升
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    © 2024 tsingfun.com, Inc.  沪ICP备2020034476号-1  沪公网安备31011702000040号

    GMT+8, 2024-05-11 17:48 , Processed in 0.018372 second(s), 36 queries .