我正在讀取以Windows格式存儲爲相對路徑的數據庫中的路徑名,並嘗試在Unix系統上從它們創建boost :: filesystem ::路徑。會發生什麼是構造函數調用將整個字符串解釋爲文件名。我需要將路徑轉換爲正確的Posix路徑,因爲它將在本地使用。boost :: filesystem - 如何從posix plattforms上的Windows路徑字符串創建加速路徑?
我沒有在boost :: filesystem參考中找到任何轉換函數,也沒有通過google找到。我只是失明瞭,是否有明顯的解決方案?如果不是,你會如何做到這一點?
例子:
std::string win_path("foo\\bar\\asdf.xml");
std::string posix_path("foo/bar/asdf.xml");
// loops just once, as part is the whole win_path interpreted as a filename
boost::filesystem::path boost_path(win_path);
BOOST_FOREACH(boost::filesystem::path part, boost_path) {
std::cout << part << std::endl;
}
// prints each path component separately
boost::filesystem::path boost_path_posix(posix_path);
BOOST_FOREACH(boost::filesystem::path part, boost_path_posix) {
std::cout << part << std::endl;
}
編輯:當然我只需更換反斜槓,但有說, 「只是工作(TM)」 適用於Windows和Unix plattforms的解決方案?
不幸的是,我可能不會更改數據庫中的格式,所以我必須去替換反斜槓...... – VolkA 2010-06-02 13:42:03