我做了一次Google搜索,看看如何檢查給定路徑是否有效,最好使用boost。boost :: filesystem :: native路徑的預期形式是什麼?
它把我帶到這裏:
How to check if path is valid in boost::filesystem?
太好了!我對自己說。 我那麼谷歌了升壓DOC這裏: http://www.boost.org/doc/libs/1_62_0/libs/filesystem/doc/portability_guide.htm
然後我寫我自己的測試:
#include <iostream>
#include <sstream>
#include <boost/filesystem.hpp>
int main()
{
const std::string test1 = "D:\\Programing Projects\\Git Workspace\\Common\\x64\\Debug";
const std::string test2 = "D:\\Programing Projects\\Git Workspace\\Common\\x64\\Debug\\";
const std::string test3 = "D:/Programing Projects/Git Workspace/Common/x64/Debug";
const std::string test4 = "D:/Programing Projects/Git Workspace/Common/x64/Debug/";
if (!boost::filesystem::native(test1))
{
std::cout << "Boost says the following path is not valid for the native operating system: " << test1 << std::endl;
}
if (!boost::filesystem::native(test2))
{
std::cout << "Boost says the following path is not valid for the native operating system: " << test2 << std::endl;
}
if (!boost::filesystem::native(test3))
{
std::cout << "Boost says the following path is not valid for the native operating system: " << test3 << std::endl;
}
if (!boost::filesystem::native(test4))
{
std::cout << "Boost says the following path is not valid for the native operating system: " << test4 << std::endl;
}
return 0;
}
測試的輸出:
Boost says the following path is not valid for the native operating system: D:\Programing Projects\Git Workspace\Common\x64\Debug
Boost says the following path is not valid for the native operating system: D:\Programing Projects\Git Workspace\Common\x64\Debug\
Boost says the following path is not valid for the native operating system: D:/Programing Projects/Git Workspace/Common/x64/Debug
Boost says the following path is not valid for the native operating system: D:/Programing Projects/Git Workspace/Common/x64/Debug/
什麼是錯路,它說,它對我的本機Windows 10操作系統無效?
這裏「有效」是什麼意思? –
@Nicol Bolas對我有效意味着路徑不一定必須指向一個文件或目錄,但是它可以用於指向一個文件或目錄,一旦它被創建。無論如何,這正是我正在尋找的。基本上,拒絕像「%&?!」這樣的東西根本不像%^的路徑!「 –
字符串'test2'是否故意包含'\ P'而不是'\\ P'? –