gpt4 book ai didi

c++ - Windows:文件重命名和目录迭代冲突

转载 作者:行者123 更新时间:2023-11-28 08:27:29 26 4
gpt4 key购买 nike

我正在使用 boost::filesystem 重命名这样的文件:

boost::filesystem::rename(tmpFileName, targetFile);

tmpFileName/targetFile 属于 boost::filsystem::path 类型。

在执行此操作时,我在另一个线程中使用此代码遍历目录:

directory_iterator end_itr;
for (directory_iterator itr(dirInfoPath); itr != end_itr; ++itr)
{
path currentPath = itr->path();
if (is_directory(itr->status()))
{
// skip directories
}
else
{
std::string file_name = currentPath.leaf();
if (!boost::algorithm::starts_with(file_name, "new")
&& !boost::algorithm::starts_with(file_name, "finished")
&& boost::algorithm::ends_with(file_name, ".info"))
{
// save found filename in some variable
return true;
}
}
}

执行这段代码时,重命名时出现异常:

boost::filesystem::rename: The process cannot access the file because it is being used by another process

迭代和重命名操作是否有可能发生冲突,因为它们都访问目录 inode,还是我有其他问题?

最佳答案

您提供的代码不包含任何文件打开操作,因此它不能锁定文件。您遍历 directory 并重命名 file,对吗?所以这个文件可能确实被另一个应用程序使用,比如文件查看器或其他东西,这是非常典型的错误。或者您已在其他地方的应用中打开了它

关于c++ - Windows:文件重命名和目录迭代冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3405331/

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