2010-11-17 273 views
2

我编写了下面的例程,以便将目录中的所有文件复制到子目录 ,然后删除它们,但我一直在copy_fail上获取拒绝访问看起来误导我。路径为更正,文件存在,权限在刚刚创建的目标目录中不是只读的。Boost:copy_file失败,访问被拒绝但没有权限问题

任何建议如何寻找问题的根源?

我试图调试,但我没有boost :: filesystem源代码。

任何建议表示赞赏。

void 
moveConfigurationFileToSubDirectory() 
{ 
// TODO: Catch errors. 

boost::filesystem::path full_path(boost::filesystem::current_path()); 

// Create directory subdir if not exist 
boost::filesystem::path subdirPath(kSubdirectory); 
    if (!boost::filesystem::exists(subdirPath)) 
{ 
    PLog::DEV.Development(devVerbose, "%s: creating directory %s", __FUNCTION__, subdirPath.string()); 
    boost::filesystem::create_directories(subdirPath); 
} else 
    PLog::DEV.Development(devVerbose, "%s: directory %s exist", __FUNCTION__, subdirPath.string()); 

// Iterate through the configuration files defined in the static array 
// copy all files with overwrite flag, if successfully delete file (looks like there is not remove) 
for (int i = 0; i < kNumberOfConfigurationFiles; i++) 
{ 
    boost::filesystem::path currentConfigurationFile(kConfigurationFiles[i]); 

    try 
    { 
    boost::filesystem::copy_file(currentConfigurationFile, subdirPath, boost::filesystem::copy_option::overwrite_if_exists); 
    boost::filesystem::remove(currentConfigurationFile); 
    } 
    catch (exception& e) 
    { 
    PLog::DEV.Development(devError, "%s: exception - %s", __FUNCTION__, e.what()); 
    } 
} 
} 

回答

0

什么操作系统正在运行此?如果在Linux/Unix上,那么你是否考虑过保存你的源文件的目录的权限(你正在删除currentConfigurationFile,这意味着保存该文件的目录必须具有写权限)?

+0

我在窗户上。我通过使用Windows API重写了相同的功能,并且它可以工作。目标参数是否需要是文件或目录?谢谢。 – emitrax 2010-11-18 08:54:07

5

你必须为subdirPath指定你想要的文件名,而不仅仅是路径。 boost的copy_file不够聪明,无法知道通过指定目录名称,您希望该文件具有与源相同的名称。

+2

为什么不将这标记为答案?帮了我很多,谢谢;) – 2014-12-17 17:29:59