gpt4 book ai didi

c++ - DeleteFile 在最近关闭的文件上失败

转载 作者:可可西里 更新时间:2023-11-01 17:01:14 25 4
gpt4 key购买 nike

我有一个单线程程序(C++、Win32、NTFS),它首先创建一个相当长的临时文件,关闭它,打开以供读取,读取,再次关闭并尝试使用 DeleteFile() 删除>.

通常一切顺利,但有时 DeleteFile() 会失败,并且 GetLastError() 会返回 ERROR_ACCESS_DENIED。文件肯定不是只读的。它发生在任何大小的文件上,但概率随着文件大小的增加而增加。

有什么想法可能锁定了文件吗?我尝试使用 WinInternals 工具进行检查,没有发现任何可疑的东西。

最佳答案

Windows 因这个问题而臭名昭著。 SQLite 通过每 100 毫秒重试一次删除操作来处理这个问题,直到达到最大次数。

我相信,如果您确定没有打开的句柄,那么在您的实现中执行此操作将使您在防病毒软件打开文件时省去一些麻烦。

作为引用,来自 SQLite 源的注释:

/*                                                                     
** Delete the named file.
**
** Note that windows does not allow a file to be deleted if some other
** process has it open. Sometimes a virus scanner or indexing program
** will open a journal file shortly after it is created in order to do
** whatever it does. While this other process is holding the
** file open, we will be unable to delete it. To work around this
** problem, we delay 100 milliseconds and try to delete again. Up
** to MX_DELETION_ATTEMPTs deletion attempts are run before giving
** up and returning an error.
*/

关于c++ - DeleteFile 在最近关闭的文件上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1753209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com