文件搜索查找功能VC++
2015-02-02 17:11
274 查看
1.搜索指定文件夹下的文件名和路径
View Code
更多可以参看:/article/1303811.html
#undef UNICODE #include <iostream> #include <string> #include <vector> #include <memory> #include <cstring> #include <windows.h> std::shared_ptr<std::vector<std::string> > fileList(const std::string& folder_path) { static std::shared_ptr<std::vector<std::string> > folder_files(new std::vector<std::string>); //返回指针, 需要迭代使用 WIN32_FIND_DATA FindData; HANDLE hError; int file_count(0); std::string file_path(folder_path); //路径名 std::string full_file_path; //全路径名 file_path.append("/*.*"); hError = FindFirstFile(file_path.c_str(), &FindData); if (hError == INVALID_HANDLE_VALUE) { std::cout << "failed to search files." << std::endl; return nullptr; } while(FindNextFile(hError, &FindData)) { //过虑".", "..", "-q" if(0 == strcmp(FindData.cFileName, ".") || 0 == strcmp(FindData.cFileName, "..") || 0 == strcmp(FindData.cFileName, "-q")) { continue; } //完整路径 full_file_path.append(folder_path); full_file_path.append("/"); full_file_path.append(FindData.cFileName); ++file_count; if (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){ //std::cout << file_count << " " << full_file_path << "<Dir>" << std::endl; fileList(full_file_path); }else{ folder_files->push_back(full_file_path); //std::cout << file_count << " " << full_file_path << std::endl; } full_file_path.clear(); //清空目录 } return folder_files; } int main(void) { std::shared_ptr<std::vector<std::string> > folder_files; folder_files = fileList("E:/bf"); if (folder_files) { for (size_t i=0; i != folder_files->size(); ++i) { std::cout << i+1 << " : " << (*folder_files)[i] << std::endl; } } return 0; }
View Code
#include<afx.h> #include<stdio.h> #include<string.h> #include<iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { TCHAR filePath[255]=L"*.*"; CFileFind finder; BOOL bFinderResult = finder.FindFile(filePath); //std::cout << filePath << std::endl; while (bFinderResult!=false) { bFinderResult=finder.FindNextFileW(); CString szRepeter = finder.GetFileName(); //const int iSize = szRepeter.GetLength(); char bufFileName[255]; WideCharToMultiByte(0, 0, szRepeter, 255, bufFileName, 255, NULL, false); //fFileName = (LPCTSTR)szRepeter; std::cout << bufFileName << std::endl; } system("pause"); return 0; } //注意要设置共享MFC中使用.
更多可以参看:/article/1303811.html
相关文章推荐
- 了解使用Win7搜索查找文件功能
- 如何用VC60实现类似windows搜索文件的功能,针对文件夹中包含的文件名进行模糊查
- 查找该目录下指定文件类型的用 vc 6.0 编写 的关于文件搜索的例子
- windowsXP系统搜索功能查找文件与文件夹的具体步骤图解
- 如何找回Win7丢失的搜索功能以便快速查找文件
- [转]VC中常用文件功能函数收集
- VB查找硬盘文件(全硬搜索)
- .Net/C# 封装磁盘目录文件搜索功能的工具类 (实现了与搜索相关的事件,以便插入客户处理代码)
- vc查找指定目录下的文件:
- VC++中如何遍历整个目录树查找文件
- Linux文件搜索查找
- [转]VC中常用文件功能函数收集
- 一个查找文件的类:它的长处在于能够搜索子目录并且是可控制的
- VC++错误,fatal error C1010,在查找预编译头指令时遇到意外的文件结尾
- 实现windows查找文件的暂停功能
- VC++错误,在查找预编译头指令时遇到意外的文件结尾
- 抢先体验百度文件搜索功能
- VC错误解决:在查找预编译头指令时遇到意外的文件结尾
- .Net/C# 封装磁盘目录文件搜索功能的工具类 (实现了与搜索相关的事件,以便插入客户处理代码)
- VC++错误,fatal error C1010,在查找预编译头指令时遇到意外的文件结尾