查找某一目录下的文件

来源:互联网 发布:2016 淘宝最近怎么了 编辑:程序博客网 时间:2024/04/29 17:02
#include <vector>#include <string>#include <Windows.h>#include <iostream>// 编译器设置的字符集, 设为 未设置void FindFile(const char* directory, std::vector<std::string>& vecPath_){vecPath_.clear(); // 注意,应该先清理下.WIN32_FIND_DATA FindFileData;HANDLE hFind;TCHAR tmpPath[MAX_PATH];// 开始查找strcpy_s(tmpPath, sizeof(tmpPath), directory);strcat_s(tmpPath, sizeof(tmpPath), "\\*.*");//查找条件hFind = FindFirstFile(tmpPath, &FindFileData);if (hFind == INVALID_HANDLE_VALUE) {return;} else {do{if (strncmp(FindFileData.cFileName,".",1)==0){continue;}strcpy_s(tmpPath, sizeof(tmpPath), directory);strcat_s(tmpPath, sizeof(tmpPath), FindFileData.cFileName);vecPath_.push_back(std::string(tmpPath));}while (FindNextFile(hFind, &FindFileData) != 0);}// 查找结束FindClose(hFind);}int _tmain(int argc, _TCHAR* argv[]){std::vector<std::string> vecPath_;FindFile("c:\\", vecPath_);int nFileSize = vecPath_.size();for (int i = 0; i < nFileSize; ++i){std::cout << vecPath_[i].c_str() << std::endl; }return 0;}

原创粉丝点击