搜索

首页  >  问答  >  正文

c++如何查找一个文件目录下所有文件?

c++如何查找一个文件目录下所有文件?

PHP中文网PHP中文网2773 天前459

全部回复(3)我来回复

  • 天蓬老师

    天蓬老师2017-04-17 14:31:41

    雷雷

    回复
    0
  • 天蓬老师

    天蓬老师2017-04-17 14:31:41

    给一个windows下的实现:

    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <windows.h>
    #include "io.h"
    using namespace std;
    char path[1000];
    int len_path=0;
    
    void get_info()
    {
        path[len_path]='*',path[len_path+1]='rrreee';
        _finddatai64_t fileinfo;
        intptr_t handle=_findfirsti64(path,&fileinfo);
        if(handle==-1) return;
        while(!_findnexti64(handle,&fileinfo))
        {
            char *name=fileinfo.name;
            if(strcmp(name,".")==0||strcmp(name,"..")==0) continue;
            if(fileinfo.attrib&_A_SUBDIR)//递归子目录
            {
                int n=strlen(name);
                memcpy(path+len_path,name,n),len_path+=n;
                path[len_path++]='\';
                get_info();
                len_path-=n+1;
            }
            else puts(fileinfo.name);
        }
        _findclose(handle);
    }
    
    int main()
    {
        strcpy(path,"D:\");
        len_path=strlen(path);
        get_info();
    }

    回复
    0
  • 怪我咯

    怪我咯2017-04-17 14:31:41

    我以前也写过这个:district10/FindFilesWithinFolder: Find and generate a file list of the folder.。

    效果:

    ➜  build git:(master) ✗ ./FindFilesWithinFolder CMakeFiles
    CMakeFiles/CMakeDirectoryInformation.cmake
    CMakeFiles/Makefile2
    CMakeFiles/progress.marks
    CMakeFiles/TargetDirectories.txt
    CMakeFiles/Makefile.cmake
    CMakeFiles/cmake.check_cache
    CMakeFiles/CMakeOutput.log
    CMakeFiles/3.0.2/CMakeDetermineCompilerABI_CXX.bin
    CMakeFiles/3.0.2/CMakeCCompiler.cmake
    CMakeFiles/3.0.2/CMakeDetermineCompilerABI_C.bin
    CMakeFiles/3.0.2/CMakeSystem.cmake
    CMakeFiles/3.0.2/CMakeCXXCompiler.cmake
    CMakeFiles/FindFilesWithinFolder.dir/link.txt
    CMakeFiles/FindFilesWithinFolder.dir/main.cpp.o
    CMakeFiles/FindFilesWithinFolder.dir/CXX.includecache
    CMakeFiles/FindFilesWithinFolder.dir/cmake_clean.cmake
    CMakeFiles/FindFilesWithinFolder.dir/DependInfo.cmake
    CMakeFiles/FindFilesWithinFolder.dir/FindFiles.cpp.o
    CMakeFiles/FindFilesWithinFolder.dir/depend.make
    CMakeFiles/FindFilesWithinFolder.dir/depend.internal
    CMakeFiles/FindFilesWithinFolder.dir/progress.make
    CMakeFiles/FindFilesWithinFolder.dir/build.make
    CMakeFiles/FindFilesWithinFolder.dir/flags.make
    CMakeFiles/3.0.2/CompilerIdC/a.out
    CMakeFiles/3.0.2/CompilerIdC/CMakeCCompilerId.c
    CMakeFiles/3.0.2/CompilerIdCXX/CMakeCXXCompilerId.cpp
    CMakeFiles/3.0.2/CompilerIdCXX/a.out

    回复
    0
  • 取消回复