C++ 使用 boost::filesystem::path 获取绝对路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12643880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:28:13  来源:igfitidea点击:

Get absolute path with boost::filesystem::path

c++boostpathfilesystems

提问by Mario

My current working directory is located at /home/myuser/program, I created a boost::filesystem::pathobject pointing to it. I appended /../somedirso it becomes /home/myuser/program/../somedir. But I need to get its resolved absolute path, which would be /home/myuser/somedir.

我当前的工作目录位于/home/myuser/program,我创建了一个boost::filesystem::path指向它的对象。我附加/../somedir所以它变成/home/myuser/program/../somedir. 但我需要得到它解析的绝对路径,这将是/home/myuser/somedir.

I have been trying for long time and I do not find any method in their referenceto do this. There is a method called make_absolute, which seems to be supposed to do what I expect, but I have to give it a “root” path argument. Which should it be? do I really need to do this to get the real absolute path? Is there any other way?

我已经尝试了很长时间,但在他们的参考资料中我没有找到任何方法来做到这一点。有一个方法叫做make_absolute,它似乎应该按照我的预期做,但我必须给它一个“根”路径参数。应该是哪个?我真的需要这样做才能获得真正的绝对路径吗?有没有其他办法?

采纳答案by Rob Kennedy

You say you want an absolute path, but your example shows that you already have an absolute path. The process of removing the ..components of a path is known as canonicalization. For that, you should call canonical. It happens to also perform the task of absolute, so you don't need to call absoluteor make_absolutefirst. The make_absolutefunction requires a base path; you can pass it current_path()if you don't have anything better.

你说你想要一个绝对路径,但你的例子表明你已经有了一个绝对路径。删除..路径组件的过程称为规范化。为此,您应该调用canonical. 它恰好也执行 的任务absolute,因此您无需先调用absolutemake_absolute。该make_absolute函数需要一个基本路径;current_path()如果你没有更好的东西,你可以通过它。

回答by Daniel

Update, since this still appears to be Google's top hit concerning absolute paths:

更新,因为这似乎仍然是谷歌关于绝对路径的热门话题:

As of Boost 1.57, some of the previously suggested functions have since been removed.

从 Boost 1.57 开始,一些先前建议的功能已被删除。

The solution that worked for me was

对我有用的解决方案是

boost::filesystem::path canonicalPath = boost::filesystem::canonical(previousPath, relativeTo);

(using the free-standing method canonical(), defined in boost/filesystem/operations.hpp, which is automatically included via boost/filesystem.hpp)

(使用在 boost/filesystem/operations.hpp 中定义的独立方法canonical(),它通过 boost/filesystem.hpp 自动包含)

Important: calling canonical on a path that does not exist (e.g., you want to create a file) will throw an exception. In that case, your next best bet is probably boost::filesystem::absolute(). It will also work for non-existing paths, but won't get rid of dots in the middle of the path (as in a/b/c/../../d.txt). Note: Make sure relativeTo refers to a directory, calling parent_path() on paths referring to files (e.g. the opened file that contained a directory or file path relative to itself).

重要提示:在不存在的路径上调用规范(例如,您要创建文件)将引发异常。在这种情况下,您的下一个最佳选择可能是 boost::filesystem::absolute()。它也适用于不存在的路径,但不会去掉路径中间的点(如 a/b/c/../../d.txt)。注意:确保relativeTo 引用一个目录,在引用文件的路径上调用parent_path()(例如,包含目录的打开文件或相对于自身的文件路径)。

回答by Mr. Llama

The documentation showsthat the make_absolutehas an optionalsecond parameter that defaults to your current path:

文件显示,该make_absolute有一个可选的第二个参数的默认值是当前路径:

path absolute(const path& p, const path& base=current_path());

path absolute(const path& p, const path& base=current_path());

Try it without the second parameter and see if it returns the results you're looking for.

在没有第二个参数的情况下尝试它,看看它是否返回您正在寻找的结果。

回答by Nicol Bolas

I have to give it a “root” path argument.

我必须给它一个“根”路径参数。

Check the docs:you don't haveto give it anything; it has a default second parameter. Namely, the current directory.

检查文档:你不要给它任何东西; 它有一个默认的第二个参数。即当前目录。

Relative paths are relative to some directory. Thus, when making a path absolute, you need to know what it should be absolute relative to. That's the "root path": the directory it is relative to.

相对路径是相对于某个目录的。因此,在使路径成为绝对路径时,您需要知道它相对于. 那是“根路径”:它相对的目录。