使用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. Node.js:相对路径和绝对路径
- 5. 绝对路径和相对路径
- 6. 改变相对路径绝对路径
- 7. 什么决定了InstanceDir是全路径还是相对路径?
- 8. 相对v绝对路径?
- 9. 相对和绝对路径
- 10. 绝对与相对路径
- 11. 使用绝对路径或相对路径阵营路线
- 12. 绝对路径
- 13. 检查是否存在具有绝对路径和相对路径的文件
- 14. 用于确定路径是相对还是绝对的Win32 API?
- 15. 网络路径使绝对路径 - asp.net
- 16. 地图木偶路径绝对路径
- 17. javax.jcr.RepositoryException:不是相对路径
- 18. 相对路径
- 19. 相对路径
- 20. 相对路径
- 21. 相对路径
- 22. 相对路径
- 23. 相对路径
- 24. PHP转换外部相对路径绝对路径
- 25. VB.NET中绝对路径的相对路径
- 26. 将相对url路径解析为其绝对路径
- 27. Elisp中绝对路径的相对路径
- 28. preg_replace链接相对路径为绝对路径
- 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