![](/img/trans.png)
[英]boost::filesystem::file_size error when using a path with whitespace
[英]boost::filesystem::path.parent_path() and whitespace
我想检查用户输入的路径。 输入必须提供不存在的文件的路径。 此外,如果输入还提供目录,则这些目录必须存在。
输入示例:
/existent_dir_a/existent_dir_b/existent_file.bin
<- /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_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);
}
}
使用!output.parent_path().empty()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.