154 lines
14 KiB
HTML
154 lines
14 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="zh" dir="ltr" class="client-nojs" xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh">
|
|
<head><meta http-equiv="x-ua-compatible" content="ie=edge">
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
<title>std::filesystem::rename</title>
|
|
<link rel="stylesheet" href="ext.css" />
|
|
<link rel="stylesheet" href="site_modules.css" />
|
|
</head>
|
|
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-cpp_filesystem_rename skin-cppreference2 action-view cpp-navbar">
|
|
<div id="cpp-content-base">
|
|
<div id="content"><a id="top"></a>
|
|
<h1 id="firstHeading" class="firstHeading"><span style="font-size:0.7em; line-height:130%">std::filesystem::</span>rename</h1>
|
|
<div id="bodyContent">
|
|
<div id="contentSub"><span class="subpages">< <a href="cpp.html">cpp</a>‎ | <a href="cpp-filesystem.html">filesystem</a></span></div>
|
|
<div id="mw-content-text" lang="zh" dir="ltr" class="mw-content-ltr" xml:lang="zh">
|
|
<table class="t-dcl-begin">
|
|
<tbody>
|
|
<tr class="t-dsc-header">
|
|
<td>
|
|
<div>定义于头文件 <code><a href="cpp-header-filesystem.html"><filesystem></a></code></div>
|
|
</td>
|
|
<td></td>
|
|
<td></td>
|
|
</tr>
|
|
<tr class="t-dcl t-since-cxx17">
|
|
<td>
|
|
<div><span class="mw-geshi cpp source-cpp"><span class="kw4">void</span> rename<span class="br0">(</span><span class="kw4">const</span> <a href="cpp-filesystem-path.html"><span class="kw2199">std::<span class="me2">filesystem</span><span class="sy4">::</span><span class="me2">path</span></span></a><span class="sy3">&</span> old_p,<br /></span>
|
|
<p> <span class="kw4">const</span> <a href="cpp-filesystem-path.html"><span class="kw2199">std::<span class="me2">filesystem</span><span class="sy4">::</span><span class="me2">path</span></span></a><span class="sy3">&</span> new_p<span class="br0">)</span><span class="sy4">;</span><br />
|
|
<span class="kw4">void</span> rename<span class="br0">(</span><span class="kw4">const</span> <a href="cpp-filesystem-path.html"><span class="kw2199">std::<span class="me2">filesystem</span><span class="sy4">::</span><span class="me2">path</span></span></a><span class="sy3">&</span> old_p,<br />
|
|
<span class="kw4">const</span> <a href="cpp-filesystem-path.html"><span class="kw2199">std::<span class="me2">filesystem</span><span class="sy4">::</span><span class="me2">path</span></span></a><span class="sy3">&</span> new_p,<br /></p>
|
|
<a href="cpp-error-error_code.html"><span class="kw877">std::<span class="me2">error_code</span></span></a><span class="sy3">&</span> ec<span class="br0">)</span><span class="sy4">;</span></div>
|
|
</td>
|
|
<td class="t-dcl-nopad"></td>
|
|
<td><span class="t-mark-rev t-since-cxx17">(C++17 起)</span></td>
|
|
</tr>
|
|
<tr class="t-dcl-sep">
|
|
<td></td>
|
|
<td></td>
|
|
<td></td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
<p>移动或重命名 <code>old_p</code> 所标识的文件系统对象到 <code>new_p</code> ,如同用 POSIX <a rel="nofollow" class="external text" href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/rename.html">rename</a> :</p>
|
|
<ul>
|
|
<li>若 <code>old_p</code> 是非目录文件,则 <code>new_p</code> 必须是下列之一:</li>
|
|
</ul>
|
|
<dl>
|
|
<dd>
|
|
<ul>
|
|
<li>与 <code>old_p</code> 为相同文件,或到它的硬链接:此情况不做任何事</li>
|
|
<li>既存的非目录文件: <code>new_p</code> 首先被删除,然后因为删除不允许其他进程观测 <code>new_p</code> ,并将路径名 <code>new_p</code> 链接到该文件,而 <code>old_p</code> 从该文件接触链接。要求含有 <code>old_p</code> 的目录和含有 <code>new_p</code> 的目录的写权限。</li>
|
|
<li>既存目录中的不存在文件:链接路径名 <code>new_p</code> 到该文件,并从该文件解链接 <code>old_p</code> 。要求含有 <code>old_p</code> 的目录和含有 <code>new_p</code> 的目录的写权限。</li>
|
|
</ul>
|
|
</dd>
|
|
</dl>
|
|
<ul>
|
|
<li>若 <code>old_p</code> 是目录,则 <code>new_p</code> 必须是下列之一:</li>
|
|
</ul>
|
|
<dl>
|
|
<dd>
|
|
<ul>
|
|
<li>与 <code>old_p</code> 为相同文件或到它的硬链接:此情况不做任何事</li>
|
|
<li>既存目录:在 POSIX 系统上若 <code>new_p</code> 为空则删除之,但这在其他系统上可能是错误。若费错误,则首先删除 <code>new_p</code> ,然后因为删除不允许其他进程观测 <code>new_p</code> ,并将路径名 <code>new_p</code> 链接到该目录,将 <code>old_p</code> 从该目录解除链接。要求含有 <code>old_p</code> 的目录和含有 <code>new_p</code> 的目录的写权限。</li>
|
|
<li>不存在的目录,不以目录分隔符终止,且亲目录存在:将路径名 <code>new_p</code> 链接到该目录,且从该目录解链接 <code>old_p</code> 。要求含有 <code>old_p</code> 的目录和含有 <code>new_p</code> 的目录的写权限。</li>
|
|
</ul>
|
|
</dd>
|
|
</dl>
|
|
<ul>
|
|
<li>不跟随符号链接:若 <code>old_p</code> 是符号链接,则重命名它自身,而非其目标。若 <code>new_p</code> 是既存符号链接,则删除它自身,而非其目标。</li>
|
|
</ul>
|
|
<p>若有如下情况则重命名失败</p>
|
|
<ul>
|
|
<li><code>new_p</code> 以 <span class="t-spar">dot</span> 或 <span class="t-spar">dot-dot</span> 结束</li>
|
|
<li><code>new_p</code> 以目录分隔符结尾,指名一个不存在的目录</li>
|
|
<li><code>old_p</code> 是一个目录,其祖先是 <code>new_p</code></li>
|
|
</ul>
|
|
<h3><span class="mw-headline" id=".E5.8F.82.E6.95.B0">参数</span></h3>
|
|
<table class="t-par-begin">
|
|
<tr class="t-par">
|
|
<td>old_p</td>
|
|
<td>-</td>
|
|
<td>要移动或重命名的路径</td>
|
|
</tr>
|
|
<tr class="t-par">
|
|
<td>new_p</td>
|
|
<td>-</td>
|
|
<td>移动/重命名操作的目标路径</td>
|
|
</tr>
|
|
<tr class="t-par">
|
|
<td>ec</td>
|
|
<td>-</td>
|
|
<td>不抛出重载中报告错误的输出参数</td>
|
|
</tr>
|
|
</table>
|
|
<h3><span class="mw-headline" id=".E8.BF.94.E5.9B.9E.E5.80.BC">返回值</span></h3>
|
|
<p>(无)</p>
|
|
<h3><span class="mw-headline" id=".E5.BC.82.E5.B8.B8">异常</span></h3>
|
|
<p>不接受 <span class="t-c"><span class="mw-geshi cpp source-cpp"><a href="cpp-error-error_code.html"><span class="kw877">std::<span class="me2">error_code</span></span></a><span class="sy3">&</span></span></span> 参数的重载在底层 OS API 错误时抛出 <a href="cpp-filesystem-filesystem_error.html"><tt>filesystem_error</tt></a> ,以第一 path 参数 <code>old_p</code>,第二 path 参数 <code>new_p</code> 和作为错误码参数的 OS 错误码构造。若 OS API 调用失败,则接受 <span class="t-c"><span class="mw-geshi cpp source-cpp"><a href="cpp-error-error_code.html"><span class="kw877">std::<span class="me2">error_code</span></span></a><span class="sy3">&</span></span></span> 参数的重载设置该参数为 OS API 错误码,而若不出现错误则执行 <span class="t-c"><span class="mw-geshi cpp source-cpp">ec.<span class="me1">clear</span><span class="br0">(</span><span class="br0">)</span></span></span> 。若内存分配失败,则任何不标记为 <code>noexcept</code> 的重载可能抛出 <span class="t-lc"><a href="cpp-memory-new-bad_alloc.html">std::bad_alloc</a></span> 。</p>
|
|
<h3><span class="mw-headline" id=".E7.A4.BA.E4.BE.8B">示例</span></h3>
|
|
<div class="t-example">
|
|
<div class="t-example-live-link"></div>
|
|
<div dir="ltr" class="mw-geshi" style="text-align: left;">
|
|
<div class="cpp source-cpp">
|
|
<pre class="de1"><span class="co2">#include <iostream></span>
|
|
<span class="co2">#include <fstream></span>
|
|
<span class="co2">#include <filesystem></span>
|
|
<span class="kw1">namespace</span> fs <span class="sy1">=</span> std<span class="sy4">::</span><span class="me2">filesystem</span><span class="sy4">;</span>
|
|
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span>
|
|
<span class="br0">{</span>
|
|
fs<span class="sy4">::</span><span class="me2">path</span> p <span class="sy1">=</span> fs<span class="sy4">::</span><span class="me2">current_path</span><span class="br0">(</span><span class="br0">)</span> <span class="sy2">/</span> <span class="st0">"sandbox"</span><span class="sy4">;</span>
|
|
fs<span class="sy4">::</span><span class="me2">create_directories</span><span class="br0">(</span>p<span class="sy2">/</span><span class="st0">"from"</span><span class="br0">)</span><span class="sy4">;</span>
|
|
<a href="cpp-io-basic_ofstream.html"><span class="kw1733">std::<span class="me2">ofstream</span></span></a><span class="br0">(</span>p<span class="sy2">/</span><span class="st0">"from/file1.txt"</span><span class="br0">)</span>.<span class="me1">put</span><span class="br0">(</span><span class="st0">'a'</span><span class="br0">)</span><span class="sy4">;</span>
|
|
fs<span class="sy4">::</span><span class="me2">create_directory</span><span class="br0">(</span>p<span class="sy2">/</span><span class="st0">"to"</span><span class="br0">)</span><span class="sy4">;</span>
|
|
|
|
<span class="co1">// fs::rename(p/"from/file1.txt", p/"to/"); // 错误: to 是目录</span>
|
|
fs<span class="sy4">::</span><span class="me2">rename</span><span class="br0">(</span>p<span class="sy2">/</span><span class="st0">"from/file1.txt"</span>, p<span class="sy2">/</span><span class="st0">"to/file2.txt"</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// OK</span>
|
|
<span class="co1">// fs::rename(p/"from", p/"to"); // 错误: to 非空</span>
|
|
fs<span class="sy4">::</span><span class="me2">rename</span><span class="br0">(</span>p<span class="sy2">/</span><span class="st0">"from"</span>, p<span class="sy2">/</span><span class="st0">"to/subdir"</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// OK</span>
|
|
|
|
fs<span class="sy4">::</span><span class="me2">remove_all</span><span class="br0">(</span>p<span class="br0">)</span><span class="sy4">;</span>
|
|
<span class="br0">}</span></pre></div>
|
|
</div>
|
|
</div>
|
|
<h3><span class="mw-headline" id=".E5.8F.82.E9.98.85">参阅</span></h3>
|
|
<table class="t-dsc-begin">
|
|
<tr class="t-dsc">
|
|
<td>
|
|
<div class="t-dsc-member-div">
|
|
<div><a href="cpp-io-c-rename.html"><span class="t-lines"><span>rename</span></span></a></div>
|
|
</div>
|
|
</td>
|
|
<td>重命名文件<br />
|
|
<span class="t-mark">(函数)</span></td>
|
|
</tr>
|
|
<tr class="t-dsc">
|
|
<td>
|
|
<div class="t-dsc-member-div">
|
|
<div><a href="cpp-filesystem-remove.html"><span class="t-lines"><span>remove</span><span>remove_all</span></span></a></div>
|
|
<div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx17">(C++17)</span></span><span><span class="t-mark-rev t-since-cxx17">(C++17)</span></span></span></div>
|
|
</div>
|
|
</td>
|
|
<td>移除一个文件或空目录<br />
|
|
移除一个文件或递归地移除一个目录及其所有内容<br />
|
|
<span class="t-mark">(函数)</span></td>
|
|
</tr>
|
|
</table>
|
|
</div>
|
|
<div class="visualClear"></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html>
|