[英]how to get the path with trailing pathseparator in c++ using boost
我有這樣的路徑:
boost::filesystem::path mypath("c:/test");
boost::filesystem::path mypath1("c:/test/);
我想確保將它們轉換為字符串時,它們都顯示為:
c:/test/
例如,如果我這樣做:
cout<<mypath.string()<<endl;
cout<<mypath1.string()<<endl;
他們兩個都打印出來:
c:/test/
我如何在C ++中使用Boost做到這一點?
我認為沒有內置函數。
您可以使用它,但是:
if ("." != p.filename())
p += fs::path::preferred_separator;
如果路徑以
/.
結尾,則不會添加分隔符/.
(可選)首先調用p.remove_trailing_separator
,但是如果這是輸入的一部分,那么也會刪除所有尾隨雙斜杠(某些應用程序將其視為具有重要意義)。
#include <boost/filesystem.hpp>
#include <boost/range/iterator_range.hpp>
#include <iostream>
namespace fs = boost::filesystem;
int main(int argc, char** argv) {
for (std::string s : boost::make_iterator_range(argv+1, argv+argc)) {
fs::path p = s;
//p.remove_trailing_separator();
if ("." != p.filename())
p += fs::path::preferred_separator;
std::cout << "'" << s << "'\t" << p << "\n";
}
}
打印(顯然在Linux上):
'.' "."
'' "/"
'/' "//"
'/tmp' "/tmp/"
'/tmp/' "/tmp/"
'/tmp//' "/tmp//"
'/tmp/.' "/tmp/."
'/tmp/..' "/tmp/../"
'/tmp/...' "/tmp/.../"
'/tmp/aa.txt' "/tmp/aa.txt/"
'c:\test.txt' "c:\test.txt/"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.