Function to remove directory removes it only after debug finishes c++

て烟熏妆下的殇ゞ 提交于 2019-12-11 14:43:41

问题


I have this code in c++ to remove directory that includes files in it:

void*  hFind = INVALID_HANDLE_VALUE;
        WIN32_FIND_DATA ffd;

        hFind = FindFirstFile((fullpath+"\\" + _docname + "\\"+"*").c_str(), &ffd);

        do //delete all the files in the directory
        {
            // check if it is a file
            if (!(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
            {
                string s = (fullpath+_docname+"\\").append(ffd.cFileName);
                remove(s.c_str());
            }
        }
        while (FindNextFile(hFind, &ffd) != 0);
        removeDirectory(fullpath+"\\" + _docname);      
        FindClose(hFind);

The problem is - the directory is actually removed only after I close the dubugger. While debugging, the directory is inaccessible, but still exists, and it make me troubles. Do you know how can I fix it to tottaly remove the folder?


回答1:


swapping the last two lines might fix this: close the handle before removing the directory

FindClose( hFind );
removeDirectory( fullpath + "\\" + _docname );       


来源:https://stackoverflow.com/questions/10832251/function-to-remove-directory-removes-it-only-after-debug-finishes-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!