【问题标题】:Boost: copy_file fail with access denied but there are no permission problemBoost:copy_file 失败,访问被拒绝,但没有权限问题
【发布时间】:2010-11-17 10:24:35
【问题描述】:

我编写了以下例程,以便将目录中的所有文件复制到子目录 然后删除它们,但我一直在 copy_fail 上被拒绝访问,这看起来对我有误导性。路径正确,文件存在且权限在刚刚创建的目标目录中不是只读的。

有什么建议可以找到问题的根源吗?

我尝试调试,但我没有 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());
  }
 }
}

【问题讨论】:

    标签: c++ boost-filesystem


    【解决方案1】:

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

    【讨论】:

    • 为什么没有将此标记为答案?帮了我很多,谢谢;)
    【解决方案2】:

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

    【讨论】:

    • 我在 Windows 上。我使用 Windows API 重写了相同的功能,它可以工作。目标参数是否需要是文件或目录?谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-10
    • 2011-02-17
    • 1970-01-01
    • 2015-01-28
    • 2015-05-24
    • 2021-08-30
    • 1970-01-01
    相关资源
    最近更新 更多