使用C++,我需要檢測給定路徑(文件名)是絕對路徑還是相對路徑。我可以使用Windows API,但不想使用Boost等第三方庫,因爲我需要小型Windows應用程序中的此解決方案,而無需依賴於附屬程序。檢測路徑是絕對路徑還是相對路徑
回答
Windows API有PathIsRelative
。它被定義爲:
BOOL PathIsRelative(
_In_ LPCTSTR lpszPath
);
嗯。我笑了一下。 – 2011-03-21 12:40:56
@LightnessRacesinOrbit:雖然它可以在99%的時間裏工作,但它不是一個完美的解決方案。這裏有兩個主要原因:1.技術上應該有三個返回選項:'是','否'和'錯誤確定'。 2.此限制:「最大長度MAX_PATH」。不幸的是,我沒有找到一個可以可靠地做到這一點的Windows API ... – ahmd0 2013-03-12 00:21:50
與開始C++ 14/C++ 17可以使用is_absolute()
和is_relative()
從filesystem library
#include <filesystem> // C++17 (or Microsoft-specific implementation in C++14)
std::string winPathString = "C:/tmp";
std::filesystem::path path(winPathString); // Construct the path from a string.
if (path.is_absolute()) {
// Arriving here if winPathString = "C:/tmp".
}
if (path.is_relative()) {
// Arriving here if winPathString = "".
// Arriving here if winPathString = "tmp".
// Arriving here in windows if winPathString = "/tmp". (see quote below)
}
的路徑 「/」 是在絕對POSIX操作系統,但在Windows上爲 。
在C++中使用14 std::experimental::filesystem
#include <experimental/filesystem> // C++14
std::experimental::filesystem::path path(winPathString); // Construct the path from a string.
我有提高1.63和VS2010(C++預C++ 11),和下面的代碼工作。在[不要把太多精力花在你的研究]
std::filesystem::path path(winPathString); // Construct the path from a string.
if (path.is_absolute()) {
// Arriving here if winPathString = "C:/tmp".
}
- 1. 檢查路徑是絕對路徑還是相對路徑
- 2. 如何在java中檢查路徑是相對還是絕對路徑
- 3. 相對路徑或絕對路徑
- 4. 絕對路徑和相對路徑
- 5. 改變相對路徑絕對路徑
- 6. Node.js:相對路徑和絕對路徑
- 7. 更改相對路徑和絕對路徑的基本路徑
- 8. 什麼是絕對路徑名和相對路徑名
- 9. 什麼決定了InstanceDir是全路徑還是相對路徑?
- 10. 絕對與相對路徑
- 11. 相對和絕對路徑
- 12. 相對v絕對路徑?
- 13. 使用絕對路徑或相對路徑陣營路線
- 14. 絕對路徑
- 15. 絕對路徑
- 16. 如何測試Elisp中的路徑是否絕對路徑?
- 17. 檢查如果Shell腳本$ 1是絕對路徑或相對路徑
- 18. 檢查是否存在具有絕對路徑和相對路徑的文件
- 19. 用於確定路徑是相對還是絕對的Win32 API?
- 20. 確定路徑是相對還是絕對
- 21. 確定路徑是本地路徑還是網絡路徑
- 22. Hadoop從絕對路徑和基本路徑獲取相對路徑
- 23. 地圖木偶路徑絕對路徑
- 24. 網絡路徑使絕對路徑 - asp.net
- 25. 將路徑轉換爲絕對路徑
- 26. javax.jcr.RepositoryException:不是相對路徑
- 27. 相對路徑
- 28. 相對路徑
- 29. 相對路徑
- 30. 相對路徑
祝賀(http://msdn.microsoft.com/en-us/library/bb773660%28v=vs.85%29.aspx)。 – 2011-03-21 12:41:19
@Tomalak Geret'kal - 你在「沒有付出多少努力」中做了什麼?無論如何,相同的鏈接已經發布爲答案,我真的很感謝你的努力,謝謝,夥計。 – 2011-03-22 06:32:17
@AlexFarber:他的觀點是,如果你嘗試過谷歌搜索,你將會把你放在正確的地方。 – 2014-03-03 16:22:58