I have a bit of code that is working except it is causing a stack overflow. I know the issue is caused because of recursion but I am not sure what I can do to stop it in this scenario.
The Code:
void FindFilesRecursively(LPCTSTR lpFolder) {
TCHAR szFullPattern[MAX_PATH];
WIN32_FIND_DATA FindFileData;
HANDLE hFindFile;
PathCombine(szFullPattern, lpFolder, (L"*"));
hFindFile = FindFirstFile(szFullPattern, &FindFileData);
if (wcscmp(FindFileData.cFileName, L".") == 0){
FindNextFile(hFindFile, &FindFileData);
if (wcscmp(FindFileData.cFileName, L"..") == 0) {
FindNextFile(hFindFile, &FindFileData);
}
}
if (hFindFile != INVALID_HANDLE_VALUE) {
do {
if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
// found a subdirectory; recurse into it
PathCombine(szFullPattern, lpFolder, FindFileData.cFileName);
printf("FOLDER: %S\n", szFullPattern);
FindFilesRecursively(szFullPattern);
}
printf("FILE: %S\n", FindFileData.cFileName);
} while (FindNextFile(hFindFile, &FindFileData));
FindClose(hFindFile);
}
printf("END\n");
}
Basically all it does it takes a folder/file path as argument and then it will print the name if it is a folder, or it will go recursively through the path and print everything. My debugger is hitting stack overflow at about 53 calls to this function. I am not sure where/how I should terminate a call to this function so that it doesn't cause the stack overflow.
'.'and'..'and an subsequent ignoring of said-same should be part of the loop; not assumed to optionally be the first (or first two) entries. As soon as the latter assumption fails, the entire loop will recurse indefinitely.hFindFileimmediately: you are executing code based on the supposed data inFindFileDatain between.MAX_PATHlpFindFileDataare indeterminate." I suggest you have a single loop, which ignores"."and"..".