c++ 遍历文件夹

方法一
使用_findfirst和_findnext

	#include 

    intptr_t hFile = -1;
    struct _finddata_t file;
    std::string pathName = "D:\\";
    if (hFile = _findfirst(pathName.append("\\*.*").c_str(), &file) != -1)
    {
        do
        {
            std::cout << file.name << ", " << file.size << "bytes\n";
        } while (_findnext(hFile, &file) == 0);
    }

    _findclose(hFile);

本人使用该方法无法遍历,hFile值为1,不明就里

方法二
WIN api

	#include 

    std::string path = "D:\\";
    WIN32_FIND_DATAA fileInfo;
    HANDLE hFile = FindFirstFileA((path + "*.*").c_str(), &fileInfo);

    if (hFile == INVALID_HANDLE_VALUE) {
        return;
    }

    do
    {
        if (strstr(fileInfo.cFileName, ".bmp"))
        {
            filePath.push_back(path + fileInfo.cFileName);
        }

    } while (FindNextFileA(hFile, &fileInfo));

不得不说winapi大法好

你可能感兴趣的:(C++/C,c++,笔记)