在最近的一个项目中,需要用c++读取6000多个文件。大家都知道,c++读取文件的能力没有Java等语言那么强大,但是在经过了一系列的尝试之后,成功了。在实施过程中将其分为两步,便于理解。
(1)获取需要读取的文件的所有文件名,并保存到filepath.txt中 ;
(2)每次从filepath.txt中读取一行作为路径,最后用一个“for循环”,或者feof()函数控制结束。
一、以下共享获取文件名的程序,保证直接可以运行。
#include "stdafx.h" #include <iostream> #include "fstream" #include "string" #include "StrmidO.h" #include <io.h> #include <vector> using namespace std; int main() { FILE *filepath; filepath = fopen("E:\\Jsonpath.txt", "a"); long Handle; struct _finddata_t FileInfo; if ((Handle = _findfirst("E:\\工程管理数据\\jsons\\*.json", &FileInfo)) == -1L) printf("没有找到匹配的项目\n"); else { printf("%s\n", FileInfo.name); fprintf(filepath, "E:\\工程管理数据\\jsons\\%s\n", FileInfo.name); while (_findnext(Handle, &FileInfo) == 0) /*printf("E:\\%s\n", FileInfo.name);*/ fprintf(filepath, "E:\\工程管理数据\\jsons\\%s\n", FileInfo.name); _findclose(Handle); } return 0; }
通过以上程序可以获取任意路径下的所有文件名,然后以自己想要的路径输出!
二、以下程序可以寻找你感兴趣的任意文件
#include "stdafx.h" #include <iostream> #include "windows.h" #include <string> #include <vector> #include <assert.h> #include <io.h> #include <fstream> using namespace std; void GetAllFormatFiles(string path, vector<string>& files, string format) { //文件句柄 long hFile = 0; //文件信息 struct _finddata_t fileinfo; string p; if ((hFile = _findfirst(p.assign(path).append("\\*" + format).c_str(), &fileinfo)) != -1) { do { if ((fileinfo.attrib & _A_SUBDIR)) { if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0) { //files.push_back(p.assign(path).append("\\").append(fileinfo.name) ); GetAllFormatFiles(p.assign(path).append("\\").append(fileinfo.name), files, format); } } else { files.push_back(p.assign(path).append("\\").append(fileinfo.name)); } } while (_findnext(hFile, &fileinfo) == 0); _findclose(hFile); } } int main() { string filePath = "E:\\1271\\1271\\"; vector<string> files; char * distAll = "E:\\AllFiles.txt"; //读取所有格式为txt的文件 string format = ".html"; GetAllFormatFiles(filePath, files, format); ofstream ofn(distAll); int size = files.size(); ofn << size << endl; for (int i = 0; i < size; i++) { ofn << files[i] << endl; cout << files[i] << endl; } ofn.close(); return 0; } BOOL SearchFilesByWildcard(WCHAR *wildcardPath) { HANDLE hFile = INVALID_HANDLE_VALUE; WIN32_FIND_DATA pNextInfo; FILE *filepath; filepath = fopen("E:\\filepath.txt", "w"); hFile = FindFirstFile(wildcardPath, &pNextInfo); if (INVALID_HANDLE_VALUE == hFile) { return FALSE; } WCHAR infPath[MAX_PATH] = { 0 }; if (pNextInfo.cFileName[0] != '.') { printf("Find result = %ws\r\n", pNextInfo.cFileName); fprintf(filepath, "E:\\\\filepath\\\\%ws\r\n", pNextInfo.cFileName); } while (FindNextFile(hFile, &pNextInfo)) { if (pNextInfo.cFileName[0] == '.') { continue; } printf("Find result = %ws\r\n", pNextInfo.cFileName); } return FALSE; }
int main(int argc, char* argv[]) { setlocale(LC_ALL, "chs"); 查找 abc开头的txt文件 printf("Search 1:\r\n"); for ( ; ; ) { if (pNextInfo.cFileName == NULL) { break; } SearchFilesByWildcard(L"E:\\Test\\1*.html"); printf("\r\n"); } 查找 abc开头的文件 printf("Search 2:\r\n"); SearchFilesByWildcard(L"E:\\Test\\abc*"); printf("\r\n"); 查找jnt文件 printf("Search 3:\r\n"); SearchFilesByWildcard(L"E:\\Test\\*.jnt"); printf("\r\n"); 查找文件名为4个字符的txt文件 printf("Search 4:\r\n"); SearchFilesByWildcard(L"E:\\Test\\????.txt"); printf("\r\n"); 查找包含"档案"的文件 printf("Search 5:\r\n"); SearchFilesByWildcard(L"E:\\Test\\*档案*"); printf("\r\n"); return 0; }