获得目录下的文件名

#include "stdafx.h"  
  
#include <fstream>  
#include <vector>  
#include <io.h>  
  
  
void getFile(std::string path, std::vector<std::string> &files )  
{  
    // 文件句柄  
    intptr_t hFile = 0; //long hFile = 0; // 64和32平台long的大小不一样  
  
    // 文件信息  
    struct _finddata_t fileinfo;  
    std::string p;  
    if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)  
    {  
        do  
        {  
            if (fileinfo.attrib & _A_SUBDIR)  
            {  
                if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)  
                    getFile(p.assign(path).append("\\").append(fileinfo.name), files);  
            }  
            else  
            {  
                files.push_back(p.assign(path).append("\\").append(fileinfo.name));  
            }  
        } while (_findnext(hFile, &fileinfo) == 0);  
        _findclose(hFile);  
    }  
  
  
}  
  
int main()  
{  
    std::vector<std::string> files;  
    getFile("C:", files);  
  
    return 0;  
}  

  

猜你喜欢

转载自www.cnblogs.com/alexYuin/p/9111527.html