如何处理boost :: fileSystem ::路径的空间
How to deal with spaces for boost::filesystem::path
我正在尝试从用户输入中获取目录,并将其存储在boost库中的路径对象中。当目录中没有空间,例如C: Windows System32 file.exe但是,当尝试使用C: Program Files file.exe时,它不起作用,该程序只是退出。我正在考虑将输入作为字符串,然后在其上操作以用逃生字符替换空格。有更好的方法吗?
boost::filesystem::path path;
std::cout << "Please enter the path for the file you would like to hash:" << std::endl;
std::cout << "E.g. C:\Program Files\iTunes\iTunes.exe" << std::endl;
std::cin >> path;
然后将路径传递到一个函数以获得哈希。对于没有空间的路径,可以正常工作,但在空间中,程序就会退出。
std::string md5_file(boost::filesystem::path &file)
{
/* Takes a file and returns the md5 hash. */
// Create new hash wrapper
hashwrapper *myWrapper = new md5wrapper();
std::string hash;
// Hash file
try
{
hash = myWrapper->getHashFromFile(file.string());
}
catch (hlException &e)
{
std::cerr << "Error(" << e.error_number() << "): " << e.error_message() << std::endl;
}
// Clean up
delete myWrapper;
return hash;
}
您的问题与boost :: filesystem ::路径无关。您的输入有问题。如果路径有空间,则cin >> string_variable
将读取到第一个白空间分离器。
尝试检查它:
[boost::filesystem::path][1] path;
std::cout << "Please enter the path for the file you would like to hash:" << std::endl;
std::cout << "E.g. C:\Program Files\iTunes\iTunes.exe" << std::endl;
std::cin >> path;
std::cout << path << endl;
输出应该是线路C:\Program
std :: getline在整个字符串中读取空格:
string str;
getline(cin, s);
path = s;
相关文章:
- 带有特殊路径部分的"std::filesystem::weakly_canonical"失败
- 在带有尾部斜杠的路径上返回 std::filesystem::create_directories() 的值
- std::filesystem 和 std::experimental::filesystem 之间的路径差异
- 从std :: fileSystem ::路径对象的类中退出时的分割故障
- 如何从std :: filesystem ::路径中删除引号
- Boost Filesystem flowly_caronical不会在没有父文件夹的情况下解决相对路径
- 如何处理boost :: fileSystem ::路径的空间
- 如果P从root路径开始,为什么STD :: filesystem :: path :: append替换当前路径
- 如何使用 boost::filesystem 检查路径是否是可创建的文件
- 为什么BOOST :: fileSystem :: path and std :: filesystem ::路径含量含
- 为什么 boost::filesystem::p ath 和 std::filesystem::p ath 的路径转义字
- 如何删除"..使用 Visual Studio 2012 类的文件路径中的 /' 令牌<filesystem>?
- 使用 boost::filesystem::p ath 获取绝对路径
- Boost filesystem::路径构造器std::length_error
- 如何将带有 ".."(向上)组件的 boost::filesystem::p ath 转换为正确的路径
- 如何使用Boost.Filesystem检查两个路径是否指向同一个文件/目录
- 查询boost::filesystem::路径长度
- 将filesystem::path元素附加到另一个路径的最佳方法是什么?
- 如何使用Boost Filesystem Library v3确定文件是否按路径包含
- 为什么boost::filesystem::canonical()要求目标路径存在?