#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
using namespace std;
int main()
{
fs::path p = fs::current_path();
cout << p << endl;
string p_string = p.string();
cout << p_string << endl;
return 0;
}
列印出“p”時,路徑顯示為這樣。
"C:\\Users\\tp\\source\\repos\\test"
但是在轉換為字串后,它是這樣的。
C:\Users\tp\source\repos\test
有沒有辦法可以保留路徑的原始形式?
uj5u.com熱心網友回復:
從cppreference 的頁面上operator<<(std::filesystem::path):
在路徑 p 上執行流輸入或輸出。
std::quoted使用,以便在以后由流輸入運算子讀取時空格不會導致截斷。
因此,我們將通過手動呼叫來獲得相同的字串std::quoted:
#include <iostream>
#include <iomanip>
#include <filesystem>
namespace fs = std::filesystem;
using namespace std;
int main()
{
fs::path p = fs::current_path();
// Should be same
cout << p << endl;
cout << std::quoted(p.string());
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/321221.html
