C++でのファイル再帰検索
Posted: 2018年11月29日(木) 14:34
C++でファイル検索のプログラムを組んでいるのですが、上手く動作してくれません。
FindFirstFileとNextFileの再帰を使っているのですが、全く動作しません。
開発環境はVisualStudio2017です。
FindFirstFileとNextFileの再帰を使っているのですが、全く動作しません。
開発環境はVisualStudio2017です。
#include<Windows.h>
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
string current = "C:\\Users\\";
char szDirectoryName[MAX_PATH];
int FindFile(string current);
int main() {
FindFile(current);
return 0;
}
int FindFile(string current) {
ofstream ofs("FileName.txt",ios::app);
WIN32_FIND_DATA date;
HANDLE handle = FindFirstFile(current.c_str(), &date);
if (handle == INVALID_HANDLE_VALUE)cout << "ファイルなし(FindFirstFile)\n" << endl;
else {
for (;;) {
if ((strcmp(date.cFileName, ".") != 0) && (strcmp(date.cFileName, "..") != 0)) {
if (date.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
GetCurrentDirectory(sizeof(szDirectoryName) / sizeof(szDirectoryName[0]), szDirectoryName);
cout << "(DirectryName)" << szDirectoryName << endl;
current = current + "\\" + date.cFileName;
cout << "(current)" << current << endl;
FindFile(current);
}
else {
cout << "(FileName)" << date.cFileName << endl;
ofs << date.cFileName << endl;
}
}
if (!FindNextFile(handle, &date)) {
if (GetLastError() == ERROR_NO_MORE_FILES) {
cout << "ファイルなし(FindNextFile)\n" << endl;
break;
}
break;
}
}
}
FindClose(handle);
ofs.close();
return 0;
}