std::experimental::filesystem:: absolute, std::experimental::filesystem:: system_complete
From cppreference.net
<
cpp
|
experimental
|
fs
|
定义于头文件
<experimental/filesystem>
|
||
|
path absolute
(
const
path
&
p,
const
path
&
base
=
current_path
(
)
)
;
|
(1) | (文件系统 TS) |
|
path system_complete
(
const
path
&
p
)
;
path system_complete ( const path & p, error_code & ec ) ; |
(2) | (文件系统 TS) |
1)
根据以下规则返回
p
相对于
base
的绝对路径:
-
- 若 p 同时具有根名称和根目录(例如 "C:\users" ),则直接返回未修改的路径。
- 若 p 具有根名称但未跟随根目录(例如 "C:text.txt" ),则在 p 的根名称与剩余部分之间插入 base 。形式化返回值为 p. root_name ( ) / fs :: absolute ( base ) . root_directory ( ) / fs :: absolute ( base ) . relative_path ( ) / p. relative_path ( ) 。
- 若 p 无根名称但具有根目录(例如 POSIX 系统上的 "/var/tmp/file.txt" 或 Windows 系统上的 "\users \A BC\Document.doc" ),则将 base 的根名称(若存在)前置到 p (在 POSIX 系统上 p 不会被修改,在 Windows 系统上 "\users \A BC\Document.doc" 会变为 "C:\users \A BC\Document.doc" )。形式化返回值为 fs :: absolute ( base ) . root_name ( ) / p 。
- 若 p 既无根名称也无根目录(例如 "../file.txt" ),则将完整的 base 前置到 p 。形式化返回值为 absolute ( base ) / p 。
2)
获取标识操作系统文件打开 API 在给定路径名
p
时将访问的文件的绝对路径。在 POSIX 系统上,这等同于使用默认
base
(
fs::current_path()
)的
(1)
操作。在 Windows 系统上,每个逻辑驱动器都有其自身的当前工作目录,因此如果
p
尚未是绝对路径且包含根名称组件(例如
"E:filename.txt"
),则使用该驱动器的当前工作目录,该目录可能由先前执行的程序设置。
目录 |
参数
| p | - | 要转换为绝对形式的路径 |
| base | - | 用作起始位置的路径(不要求是绝对路径) |
| ec | - | 非抛出重载中用于错误报告的输出参数 |
返回值
返回通过结合 p 和 base 形成的绝对(虽然不一定是规范)路径,具体方式如上所述。
异常
The overload that does not take an error_code & parameter throws filesystem_error on underlying OS API errors, constructed with p as the first argument, base as the second argument, and the OS error code as the error code argument. std:: bad_alloc may be thrown if memory allocation fails. The overload taking an error_code & parameter sets it to the OS API error code if an OS API call fails, and executes ec. clear ( ) if no errors occur. This overload has
noexcept
规范:
noexcept
注释
在支持根名称的系统(例如 Windows)上,对具有根名称的相对路径(例如
"D:file.txt"
)调用
absolute
时,若
base
的根名称不同,通常会产生一个不存在的路径。
示例
运行此代码
#include <filesystem> #include <iostream> namespace fs = std::experimental::filesystem; int main() { fs::path p = "C:cl.exe"; std::cout << "Current path is " << fs::current_path() << '\n' << "Absolute path for " << p << " is " << fs::absolute(p) << '\n' << "System complete path for " << p << " is " << fs::system_complete(p) << '\n'; }
可能的输出:
Current path is "D:/local/ConsoleApplication1" Absolute path for "C:cl.exe" is "C:/local/ConsoleApplication1/cl.exe" System complete path for "C:cl.exe" is "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\cl.exe"
参见
|
组合规范路径
(函数) |