Hey everyone I have been doing a bit of C++ programming lately and found my self stuck when I tried to port a program written in Java to C++ that uses a recursive function to delete the files in a directory.
Now for some reason it deletes the files on the first layer correctly but when I implemented the if statement to check to see if the file was a directory or not it throws my program into an endless loop. Can anyone provide me with some incite into fixing this problem?Code:#include <unistd.h> #include <string> #include <iostream> using namespace std; char* initialPath = "C:\\test\\*"; WIN32_FIND_DATA fileData; HANDLE handle; int deleteDirectories (char* initialPath, WIN32_FIND_DATA fileData, HANDLE handle) { string newFileName; string fileName; handle = FindFirstFile(initialPath, &fileData); newFileName = initialPath; newFileName = newFileName.substr(0, newFileName.size() - 1) + fileData.cFileName; remove(newFileName.c_str()); while (true) { if (fileName == fileData.cFileName) { break; } fileName = fileData.cFileName; FindNextFile(handle, &fileData); if (fileData.dwFileAttributes == 16) { newFileName = initialPath; newFileName = newFileName.substr(0, newFileName.size() - 1) + fileData.cFileName + "\\*"; initialPath = const_cast<char*>(newFileName.c_str()); cout << initialPath << endl; deleteDirectories (initialPath, fileData, handle); } newFileName = initialPath; newFileName = newFileName.substr(0, newFileName.size() - 1) + fileData.cFileName; remove(newFileName.c_str()); } FindClose(handle); return 0; } int main() { deleteDirectories (initialPath, fileData, handle); return 0; }




Reply With Quote