亲测可用
void getFiles(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) getFiles(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); } } void mian() { InitConsoleWindow1(); char * filePath = "E:/gait/004"; vector<string> files; getFiles(filePath, files); char str[30]; int size = files.size(); for (int i = 0; i < size; i++) { cout << files[i].c_str() << endl; } }
因为当时写的是个mfc框架,Initconsolewindow1()是为了能在mfc运行时输出控制台信息
void InitConsoleWindow1() { int nCrt = 0; FILE* fp; AllocConsole(); nCrt = _open_osfhandle((long)GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT); fp = _fdopen(nCrt, "w"); *stdout = *fp; setvbuf(stdout, NULL, _IONBF, 0); }
时间: 2024-10-17 11:33:38