Files
hustoj/web/cppreference-zh/chmhelp/cpp-filesystem-create_directory.html
2025-08-01 12:57:54 +08:00

198 lines
19 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::create_directory, std::filesystem::create_directories</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_create_directory 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>create_directory, <span style="font-size:0.7em; line-height:130%">std::filesystem::</span>create_directories</h1>
<div id="bodyContent">
<div id="contentSub"><span class="subpages">&lt; <a href="cpp.html">cpp</a>&lrm; | <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>&#23450;&#20041;&#20110;&#22836;&#25991;&#20214; <code><a href="cpp-header-filesystem.html">&lt;filesystem&gt;</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">bool</span> create_directory<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">&amp;</span> p <span class="br0">)</span><span class="sy4">;</span><br />
<span class="kw4">bool</span> create_directory<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">&amp;</span> p, <a href="cpp-error-error_code.html"><span class="kw877">std::<span class="me2">error_code</span></span></a><span class="sy3">&amp;</span> ec <span class="br0">)</span> <span class="kw1">noexcept</span><span class="sy4">;</span></span></div>
</td>
<td>(1)</td>
<td><span class="t-mark-rev t-since-cxx17">(C++17 &#36215;)</span></td>
</tr>
<tr class="t-dcl t-since-cxx17">
<td>
<div><span class="mw-geshi cpp source-cpp"><span class="kw4">bool</span> create_directory<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">&amp;</span> p,<br /></span>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<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">&amp;</span> existing_p <span class="br0">)</span><span class="sy4">;</span><br />
<span class="kw4">bool</span> create_directory<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">&amp;</span> p,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<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">&amp;</span> existing_p,<br /></p>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="cpp-error-error_code.html"><span class="kw877">std::<span class="me2">error_code</span></span></a><span class="sy3">&amp;</span> ec <span class="br0">)</span> <span class="kw1">noexcept</span><span class="sy4">;</span></div>
</td>
<td>(2)</td>
<td><span class="t-mark-rev t-since-cxx17">(C++17 &#36215;)</span></td>
</tr>
<tr class="t-dcl t-since-cxx17">
<td>
<div><span class="mw-geshi cpp source-cpp"><span class="kw4">bool</span> create_directories<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">&amp;</span> p <span class="br0">)</span><span class="sy4">;</span><br />
<span class="kw4">bool</span> create_directories<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">&amp;</span> p, <a href="cpp-error-error_code.html"><span class="kw877">std::<span class="me2">error_code</span></span></a><span class="sy3">&amp;</span> ec <span class="br0">)</span><span class="sy4">;</span></span></div>
</td>
<td>(3)</td>
<td><span class="t-mark-rev t-since-cxx17">(C++17 &#36215;)</span></td>
</tr>
<tr class="t-dcl-sep">
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<div class="t-li1"><span class="t-li">1)</span> &#22914;&#21516;&#29992; POSIX <a rel="nofollow" class="external text" href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/mkdir.html"><code>mkdir()</code></a> &#20197; <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw1">static_cast</span><span class="sy1">&lt;</span><span class="kw4">int</span><span class="sy1">&gt;</span><span class="br0">(</span><a href="cpp-filesystem-perms.html"><span class="kw2233">std::<span class="me2">filesystem</span><span class="sy4">::</span><span class="me2">perms</span><span class="sy4">::</span><span class="me2">all</span></span></a><span class="br0">)</span></span></span> &#20026;&#31532;&#20108;&#21442;&#25968;&#21019;&#24314;&#30446;&#24405; <code>p</code> &#65288;&#20146;&#30446;&#24405;&#24517;&#39035;&#24050;&#32463;&#23384;&#22312;&#65289;&#12290;&#33509;&#35813;&#20989;&#25968;&#22240;&#20026; <code>p</code> &#35299;&#26512;&#21040;&#26082;&#23384;&#30446;&#24405;&#32780;&#22833;&#36133;&#65292;&#21017;&#19981;&#25253;&#21578;&#38169;&#35823;&#12290;&#21542;&#21017;&#22312;&#22833;&#36133;&#26102;&#25253;&#21578;&#38169;&#35823;&#12290;</div>
<div class="t-li1"><span class="t-li">2)</span> &#21516; <span class="t-v">(1)</span> &#65292;&#38500;&#20102;&#26032;&#30446;&#24405;&#30340;&#23646;&#24615;&#22797;&#21046;&#33258; <code>existing_p</code> &#65288;&#24517;&#39035;&#26159;&#24050;&#23384;&#22312;&#30340;&#30446;&#24405;&#65289;&#12290;&#22797;&#21046;&#30340;&#23646;&#24615;&#26159;&#21462;&#20915;&#20110;&#25805;&#20316;&#31995;&#32479;&#65306;&#22312; POSIX &#31995;&#32479;&#19978;&#65292;&#22914;&#21516;&#25353;&#29031;&#19979;&#26041;&#22797;&#21046;&#23646;&#24615;
<div dir="ltr" class="mw-geshi" style="text-align: left;">
<div class="cpp source-cpp">
<pre class="de1">stat<span class="br0">(</span>existing_p.<span class="me1">c_str</span><span class="br0">(</span><span class="br0">)</span>, <span class="sy3">&amp;</span>attributes_stat<span class="br0">)</span>
mkdir<span class="br0">(</span>p.<span class="me1">c_str</span><span class="br0">(</span><span class="br0">)</span>, attributes_stat.<span class="me1">st_mode</span><span class="br0">)</span></pre></div>
</div>
&#22312; Windows &#25805;&#20316;&#31995;&#32479;&#19978;&#65292;&#19981;&#22797;&#21046; <code>existing_p</code> &#30340;&#23646;&#24615;&#12290;</div>
<div class="t-li1"><span class="t-li">3)</span> &#23545;&#27599;&#20010;&#23578;&#26410;&#23384;&#22312;&#30340; <code>p</code> &#30340;&#20803;&#32032;&#25191;&#34892; <span class="t-v">(1)</span> &#12290;&#33509; <code>p</code> &#24050;&#23384;&#22312;&#65292;&#21017;&#20989;&#25968;&#19981;&#20570;&#20219;&#20309;&#20107;&#65288;&#19981;&#25226;&#27492;&#26465;&#20214;&#24403;&#20570;&#38169;&#35823;&#65289;&#12290;</div>
<h3><span class="mw-headline" id=".E5.8F.82.E6.95.B0">&#21442;&#25968;</span></h3>
<table class="t-par-begin">
<tr class="t-par">
<td>p</td>
<td>-</td>
<td>&#35201;&#21019;&#24314;&#30340;&#26032;&#30446;&#24405;&#30340;&#36335;&#24452;</td>
</tr>
<tr class="t-par">
<td>existing_p</td>
<td>-</td>
<td>&#35201;&#33258;&#20043;&#22797;&#21046;&#23646;&#24615;&#30340;&#30446;&#24405;&#30340;&#36335;&#24452;</td>
</tr>
<tr class="t-par">
<td>ec</td>
<td>-</td>
<td>&#19981;&#25243;&#20986;&#37325;&#36733;&#20013;&#25253;&#21578;&#38169;&#35823;&#30340;&#36755;&#20986;&#21442;&#25968;</td>
</tr>
</table>
<h3><span class="mw-headline" id=".E8.BF.94.E5.9B.9E.E5.80.BC">&#36820;&#22238;&#20540;</span></h3>
<p>&#33509;&#21019;&#24314;&#20102; <code>p</code> &#25152;&#35299;&#26512;&#21040;&#30340;&#30446;&#24405;&#21017;&#20026; <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw2">true</span></span></span> &#65292;&#21542;&#21017;&#20026; <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw2">false</span></span></span> &#12290;</p>
<h3><span class="mw-headline" id=".E5.BC.82.E5.B8.B8">&#24322;&#24120;</span></h3>
<div class="t-li1"><span class="t-li">1,3)</span> &#19981;&#25509;&#21463; <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">&amp;</span></span></span> &#21442;&#25968;&#30340;&#37325;&#36733;&#22312;&#24213;&#23618; OS API &#38169;&#35823;&#26102;&#25243;&#20986; <a href="cpp-filesystem-filesystem_error.html"><tt>filesystem_error</tt></a> &#65292;&#20197;&#31532;&#19968; path &#21442;&#25968; <code>p</code> &#21644;&#20316;&#20026;&#38169;&#35823;&#30721;&#21442;&#25968;&#30340; OS &#38169;&#35823;&#30721;&#26500;&#36896;&#12290;&#33509; OS API &#35843;&#29992;&#22833;&#36133;&#65292;&#21017;&#25509;&#21463; <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">&amp;</span></span></span> &#21442;&#25968;&#30340;&#37325;&#36733;&#35774;&#32622;&#35813;&#21442;&#25968;&#20026; OS API &#38169;&#35823;&#30721;&#65292;&#32780;&#33509;&#19981;&#20986;&#29616;&#38169;&#35823;&#21017;&#25191;&#34892; <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> &#12290;&#33509;&#20869;&#23384;&#20998;&#37197;&#22833;&#36133;&#65292;&#21017;&#20219;&#20309;&#19981;&#26631;&#35760;&#20026; <code>noexcept</code> &#30340;&#37325;&#36733;&#21487;&#33021;&#25243;&#20986; <span class="t-lc"><a href="cpp-memory-new-bad_alloc.html">std::bad_alloc</a></span> &#12290;</div>
<div class="t-li1"><span class="t-li">2)</span> &#19981;&#25509;&#21463; <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">&amp;</span></span></span> &#21442;&#25968;&#30340;&#37325;&#36733;&#22312;&#24213;&#23618; OS API &#38169;&#35823;&#26102;&#25243;&#20986; <a href="cpp-filesystem-filesystem_error.html"><tt>filesystem_error</tt></a> &#65292;&#20197;&#31532;&#19968; path &#21442;&#25968; <code>p</code>&#65292;&#31532;&#20108; path &#21442;&#25968; <code>existing_p</code> &#21644;&#20316;&#20026;&#38169;&#35823;&#30721;&#21442;&#25968;&#30340; OS &#38169;&#35823;&#30721;&#26500;&#36896;&#12290;&#33509; OS API &#35843;&#29992;&#22833;&#36133;&#65292;&#21017;&#25509;&#21463; <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">&amp;</span></span></span> &#21442;&#25968;&#30340;&#37325;&#36733;&#35774;&#32622;&#35813;&#21442;&#25968;&#20026; OS API &#38169;&#35823;&#30721;&#65292;&#32780;&#33509;&#19981;&#20986;&#29616;&#38169;&#35823;&#21017;&#25191;&#34892; <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> &#12290;&#33509;&#20869;&#23384;&#20998;&#37197;&#22833;&#36133;&#65292;&#21017;&#20219;&#20309;&#19981;&#26631;&#35760;&#20026; <code>noexcept</code> &#30340;&#37325;&#36733;&#21487;&#33021;&#25243;&#20986; <span class="t-lc"><a href="cpp-memory-new-bad_alloc.html">std::bad_alloc</a></span> &#12290;</div>
<h3><span class="mw-headline" id=".E6.B3.A8.E8.A7.A3">&#27880;&#35299;</span></h3>
<p>&#20445;&#25345;&#23646;&#24615;&#30340;&#37325;&#36733; <span class="t-v">(2)</span> &#34987; <a href="cpp-filesystem-copy.html"><tt>copy()</tt></a> &#22312;&#36882;&#24402;&#22320;&#22797;&#21046;&#30446;&#24405;&#26102;&#38544;&#24335;&#35843;&#29992;&#12290;&#20854;&#22312; boost.filesystem &#30340;&#31561;&#20215;&#29289;&#26159; <a rel="nofollow" class="external text" href="http://www.boost.org/doc/libs/1_57_0/libs/filesystem/doc/reference.html#copy_directory"><code>copy_directory</code></a> &#65288;&#21442;&#25968;&#39034;&#24207;&#30456;&#21453;&#65289;</p>
<h3><span class="mw-headline" id=".E7.A4.BA.E4.BE.8B">&#31034;&#20363;</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 &lt;iostream&gt;</span>
<span class="co2">#include &lt;fstream&gt;</span>
<span class="co2">#include &lt;cstdlib&gt;</span>
<span class="co2">#include &lt;filesystem&gt;</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">current_path</span><span class="br0">(</span>fs<span class="sy4">::</span><span class="me2">temp_directory_path</span><span class="br0">(</span><span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span>
fs<span class="sy4">::</span><span class="me2">create_directories</span><span class="br0">(</span><span class="st0">&quot;sandbox/1/2/a&quot;</span><span class="br0">)</span><span class="sy4">;</span>
fs<span class="sy4">::</span><span class="me2">create_directory</span><span class="br0">(</span><span class="st0">&quot;sandbox/1/2/b&quot;</span><span class="br0">)</span><span class="sy4">;</span>
fs<span class="sy4">::</span><span class="me2">permissions</span><span class="br0">(</span><span class="st0">&quot;sandbox/1/2/b&quot;</span>, fs<span class="sy4">::</span><span class="me2">perms</span><span class="sy4">::</span><span class="me2">others_all</span>, fs<span class="sy4">::</span><span class="me2">perm_options</span><span class="sy4">::</span><span class="me2">remove</span><span class="br0">)</span><span class="sy4">;</span>
fs<span class="sy4">::</span><span class="me2">create_directory</span><span class="br0">(</span><span class="st0">&quot;sandbox/1/2/c&quot;</span>, <span class="st0">&quot;sandbox/1/2/b&quot;</span><span class="br0">)</span><span class="sy4">;</span>
<a href="cpp-utility-program-system.html"><span class="kw974">std::<span class="me2">system</span></span></a><span class="br0">(</span><span class="st0">&quot;ls -l sandbox/1/2&quot;</span><span class="br0">)</span><span class="sy4">;</span>
<a href="cpp-utility-program-system.html"><span class="kw974">std::<span class="me2">system</span></span></a><span class="br0">(</span><span class="st0">&quot;tree sandbox&quot;</span><span class="br0">)</span><span class="sy4">;</span>
fs<span class="sy4">::</span><span class="me2">remove_all</span><span class="br0">(</span><span class="st0">&quot;sandbox&quot;</span><span class="br0">)</span><span class="sy4">;</span>
<span class="br0">}</span></pre></div>
</div>
<p>&#21487;&#33021;&#30340;&#36755;&#20986;&#65306;</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;">
<div class="text source-text">
<pre class="de1">drwxr-xr-x 2 user group 4096 Apr 15 09:33 a
drwxr-x--- 2 user group 4096 Apr 15 09:33 b
drwxr-x--- 2 user group 4096 Apr 15 09:33 c
sandbox
&#9492;&#9472;&#9472; 1
&#9492;&#9472;&#9472; 2
&#9500;&#9472;&#9472; a
&#9500;&#9472;&#9472; b
&#9492;&#9472;&#9472; c</pre></div>
</div>
</div>
<h3><span class="mw-headline" id=".E7.BC.BA.E9.99.B7.E6.8A.A5.E5.91.8A">&#32570;&#38519;&#25253;&#21578;</span></h3>
<p>&#19979;&#21015;&#26356;&#25913;&#34892;&#20026;&#30340;&#32570;&#38519;&#25253;&#21578;&#36861;&#28335;&#22320;&#24212;&#29992;&#20110;&#20197;&#21069;&#20986;&#29256;&#30340; C++ &#26631;&#20934;&#12290;</p>
<table class="dsctable" style="font-size:0.8em">
<tr>
<th>DR</th>
<th>&#24212;&#29992;&#20110;</th>
<th>&#20986;&#29256;&#26102;&#30340;&#34892;&#20026;</th>
<th>&#27491;&#30830;&#34892;&#20026;</th>
</tr>
<tr>
<td><a rel="nofollow" class="external text" href="https://cplusplus.github.io/LWG/issue2935">LWG 2935</a></td>
<td>C++17</td>
<td>&#33509;&#30446;&#26631;&#24050;&#23384;&#22312;&#20294;&#38750;&#30446;&#24405;&#65292;&#21017;&#20026;&#38169;&#35823;</td>
<td>&#19981;&#20877;&#26159;&#38169;&#35823;</td>
</tr>
<tr>
<td><a rel="nofollow" class="external text" href="https://cplusplus.github.io/LWG/issue3014">LWG 3014</a></td>
<td>C++17</td>
<td><code>create_directories</code> &#30340; <code>error_code</code> &#37325;&#36733;&#34987;&#26631;&#35760;&#20026; noexcept &#20294;&#33021;&#20998;&#37197;&#20869;&#23384;</td>
<td>&#31227;&#38500; noexcept</td>
</tr>
<tr>
<td><a rel="nofollow" class="external text" href="https://wg21.link/P1164R1">P1164R1</a></td>
<td>C++17</td>
<td>&#24050;&#23384;&#22312;&#30340;&#38750;&#30446;&#24405;&#25991;&#20214;&#23548;&#33268;&#30340;&#21019;&#24314;&#22833;&#36133;&#19981;&#26159;&#38169;&#35823;</td>
<td>&#20351;&#20043;&#20026;&#38169;&#35823;</td>
</tr>
</table>
<h3><span class="mw-headline" id=".E5.8F.82.E9.98.85">&#21442;&#38405;</span></h3>
<table class="t-dsc-begin">
<tr class="t-dsc">
<td>
<div class="t-dsc-member-div">
<div><a href="cpp-filesystem-create_symlink.html"><span class="t-lines"><span>create_symlink</span><span>create_directory_symlink</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>&#21019;&#24314;&#19968;&#20010;&#31526;&#21495;&#38142;&#25509;<br />
<span class="t-mark">(&#20989;&#25968;)</span></td>
</tr>
<tr class="t-dsc">
<td>
<div class="t-dsc-member-div">
<div><a href="cpp-filesystem-copy.html"><span class="t-lines"><span>copy</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx17">(C++17)</span></span></span></div>
</div>
</td>
<td>&#22797;&#21046;&#25991;&#20214;&#25110;&#30446;&#24405;<br />
<span class="t-mark">(&#20989;&#25968;)</span></td>
</tr>
<tr class="t-dsc">
<td>
<div class="t-dsc-member-div">
<div><a href="cpp-filesystem-perms.html"><span class="t-lines"><span>perms</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx17">(C++17)</span></span></span></div>
</div>
</td>
<td>&#26631;&#35782;&#25991;&#20214;&#31995;&#32479;&#26435;&#38480;<br />
<span class="t-mark">(&#26522;&#20030;)</span></td>
</tr>
</table>
</div>
<div class="visualClear"></div>
</div>
</div>
</div>
</body>
</html>