A small problem

zhaozj2021-02-16  45

One of the inconspicuous small problems, Li Annea, for C and C programmers on May 31, 2002, must have a deep feelings for FindFirstFile () and FINDNEXTFILE (), but I don't know that I will delete one with the following function. Tree directory tree, what do you think: void removethem (char * strpath) {char strtemfile [256]; strcpy (strcpy, strpath); strcat (strcatfile, "//*.*"); Win32_find_data findfileData; handle hfind = FindFirstFile (strTemFile, & FindFileData); if (hFind = INVALID_HANDLE_VALUE!) while (TRUE) {strcpy (strTemFile, strPath); strcat (strTemFile, "//"); strcat (strTemFile, FindFileData.cFileName); if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {RemoveThem (strTemFile); // recursive call if it's a subdirectory.} else :: DeleteFile (strTemFile); // Delete it if it's a file if (FindNextFile (hFind, & FindFileData.!)) break;}: : CloseHandle (HFIND); :: RemoveDirectory (STRPATH);} The initial parameter StrPath is the root of the subdirectory tree to be deleted. From the surface, this function does not seem to have any problems, but if you really use it, it is miserable, in fact it will delete the content on the entire disk! The reason is that the first two subdirectories found in FindFirstFile () and FINDNEXTFILE () are usually "." And "..". The problem is in "..", try to plug a few ".." in a path, then what is the result ... really dare not imagine. This problem is not eye, but its consequence is not to neglect, I want everyone to pay attention to this problem, here is the main purpose to give you another wake up again, so as not to leave again. Of course, it is also very simple, as long as the following statement is added in the beginning of the While (True): IF (FindfileData.cfileName [0] == '.') {If (! FindNextFile (Hfind, & FindfileData)) Break; CONTINUE;} is to remember ".." elimination, of course, "should also be removed.

转载请注明原文地址:https://www.9cbs.com/read-25805.html

New Post(0)