2010-08-04 38 views
0

我使用boost ::文件系統重新命名一個像這樣的文件:的Windows:文件重命名和目錄迭代衝突

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

tmpFileName /的TargetFile是一個類型爲boost :: filsystem ::路徑。

雖然這樣做,我遍歷在另一個線程使用該代碼的目錄:

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,或者我還有其他問題?

回答

1

您提供的代碼不包含任何文件打開操作,所以它不能包含lock這個文件。你迭代directory並重命名file,對吧?所以有可能這個文件真的被用於另一個應用程序,比如文件查看器或其他東西,這是非常典型的錯誤。或者你在別的地方在你的應用中打開它