【问题标题】:boost::filesystem::path.parent_path() and whitespaceboost::filesystem::path.parent_path() 和空格
【发布时间】:2017-02-09 15:10:27
【问题描述】:

我想检查用户输入的路径。输入必须提供不存在的文件的路径。此外,如果输入还提供目录,则这些目录必须存在。

示例输入:

/existent_dir_a/existent_dir_b/existent_file.bin

/existent_dir_a/existent_dir_b/non_existent_file.bin

/non_existent_dir_a/non_existent_file.bin

non_existent_file.bin

existent_file.bin

下面的代码显示了一个示例。我希望它能实现我的目标,但我还有一个问题。

我怎样才能摆脱output.parent_path().string().size() != 0,因为它对我来说有点难看?

#include <boost/filesystem.hpp>
#include <iostream>
#include <string>
#include <exception>

namespace fs = boost::filesystem;

int main(int ac, char ** av)
{
  fs::path output(av[1]);

  std::cout << output << std::endl;

  std::cout << output.parent_path() << std::endl;

  if (fs::exists(output))
  {
    std::string msg = output.string() + " already exists";

    throw std::invalid_argument(msg);
  }

  if ( output.parent_path().string().size() != 0 &&
       !fs::exists(output.parent_path()) )
  {
    std::string msg = output.parent_path().string() + " is not a directory";

    throw std::invalid_argument(msg);
  }
}

【问题讨论】:

    标签: c++ boost boost-filesystem


    【解决方案1】:

    使用!output.parent_path().empty()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-02
      • 2012-07-06
      • 1970-01-01
      • 2011-04-23
      • 1970-01-01
      • 2018-02-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多