遍历文件夹下所有文件将文件名写到txt中(X86)

https://www.cnblogs.com/tgyf/p/3839894.html 

#include "io.h"
#include <fstream>
#include <string>
void getFilesAll(string path, vector<string>& files) {
	//文件句柄
	long hFile = 0;
	//文件信息
	struct _finddata_t fileinfo;
	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) {
					//files.push_back(p.assign(path).append("\\").append(fileinfo.name));
					getFilesAll(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() {
	//void getFilesAll(string path, vector<string>& files);
	char* filePath = "E:\\bolts";
	vector<string> files;
	char* distAll = "E:\\AllFiles.txt";
	getFilesAll(filePath, files);
	ofstream ofn(distAll);
	int size = files.size();
	ofn << size << endl;
	for (int i = 0; i < size; i++) {
     ofn << files[i] << endl;
			}
		ofn.close();
		return 0;
}

 

posted @ 2020-01-04 18:46  Parallax  阅读(303)  评论(0编辑  收藏  举报