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

87 lines
5.7 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::system</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_utility_program_system 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::</span>system</h1>
<div id="bodyContent">
<div id="contentSub"><span class="subpages">&lt; <a href="cpp.html">cpp</a>&lrm; | <a href="cpp-utility.html">utility</a>&lrm; | <a href="cpp-utility-program.html">program</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-cstdlib.html">&lt;cstdlib&gt;</a></code></div>
</td>
<td></td>
<td></td>
</tr>
<tr class="t-dcl">
<td class="t-dcl-nopad">
<div><span class="mw-geshi cpp source-cpp"><span class="kw4">int</span> system<span class="br0">(</span> <span class="kw4">const</span> <span class="kw4">char</span><span class="sy2">*</span> command <span class="br0">)</span><span class="sy4">;</span></span></div>
</td>
<td class="t-dcl-nopad"></td>
<td class="t-dcl-nopad"></td>
</tr>
<tr class="t-dcl-sep">
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<p>&#20197;&#21442;&#25968; <code>command</code> &#35843;&#29992;&#36816;&#34892;&#29615;&#22659;&#30340;&#21629;&#20196;&#22788;&#29702;&#22120;&#65288;&#20363;&#22914; <code>/bin/sh</code> &#12289; <code>cmd.exe</code> &#12289; <code>command.com</code> &#65289;&#12290;&#36820;&#22238;&#30456;&#24212;&#23454;&#29616;&#30340;&#23450;&#20041;&#20540;&#65288;&#36890;&#24120;&#26159;&#34987;&#35843;&#29992;&#31243;&#24207;&#25152;&#36820;&#22238;&#30340;&#20540;&#65289;&#12290;</p>
<p>&#33509; <code>command</code> &#20026;&#31354;&#65292;&#21017;&#26816;&#26597;&#36816;&#34892;&#29615;&#22659;&#26159;&#21542;&#26377;&#21629;&#20196;&#22788;&#29702;&#22120;&#65292;&#24182;&#24403;&#19988;&#20165;&#24403;&#21629;&#20196;&#34892;&#23384;&#22312;&#25165;&#36820;&#22238;&#38750;&#38646;&#12290;</p>
<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>command</td>
<td>-</td>
<td>&#26631;&#35782;&#35201;&#36816;&#34892;&#20110;&#21629;&#20196;&#22788;&#29702;&#22120;&#30340;&#21629;&#20196;&#23383;&#31526;&#20018;&#12290;&#33509;&#32473;&#20986;&#31354;&#25351;&#38024;&#65292;&#21017;&#26816;&#26597;&#21629;&#20196;&#22788;&#29702;&#22120;&#30340;&#23384;&#22312;&#24615;</td>
</tr>
</table>
<h3><span class="mw-headline" id=".E8.BF.94.E5.9B.9E.E5.80.BC">&#36820;&#22238;&#20540;</span></h3>
<p>&#23454;&#29616;&#23450;&#20041;&#20540;&#12290;&#33509; <code>command</code> &#20026;&#31354;&#25351;&#38024;&#65292;&#21017;&#24403;&#19988;&#20165;&#24403;&#21629;&#20196;&#22788;&#29702;&#22120;&#23384;&#22312;&#25165;&#36820;&#22238;&#38750;&#38646;&#20540;&#12290;</p>
<h3><span class="mw-headline" id=".E6.B3.A8.E6.84.8F">&#27880;&#24847;</span></h3>
<p>POSIX &#31995;&#32479;&#19978;&#65292;&#21487;&#29992; <a rel="nofollow" class="external text" href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/wait.html">WEXITSTATUS &#21644; WSTOPSIG</a> &#20998;&#35299;&#36820;&#22238;&#20540;&#12290;</p>
<p>&#30456;&#20851;&#30340; POSIX &#20989;&#25968; <a rel="nofollow" class="external text" href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/popen.html">popen</a> &#20351;&#35843;&#29992;&#26041;&#21487;&#33719;&#21462;&#21040; <code>command</code> &#29983;&#25104;&#30340;&#36755;&#20986;&#12290;</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;cstdlib&gt;</span>
<span class="co2">#include &lt;fstream&gt;</span>
<span class="co2">#include &lt;iostream&gt;</span>
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
std<span class="sy4">::</span><span class="me2">system</span><span class="br0">(</span><span class="st0">&quot;ls -l &gt;test.txt&quot;</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// &#25191;&#34892; UNIX &#21629;&#20196; &quot;ls -l &gt;test.txt&quot;</span>
<a href="cpp-io-cout.html"><span class="kw1757">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> <a href="cpp-io-basic_ifstream.html"><span class="kw1731">std::<span class="me2">ifstream</span></span></a><span class="br0">(</span><span class="st0">&quot;test.txt&quot;</span><span class="br0">)</span>.<span class="me1">rdbuf</span><span class="br0">(</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">total 16
-rwxr-xr-x 1 2001 2000 8859 Sep 30 20:52 a.out
-rw-rw-rw- 1 2001 2000 161 Sep 30 20:52 main.cpp
-rw-r--r-- 1 2001 2000 0 Sep 30 20:52 test.txt</pre></div>
</div>
</div>
<h3><span class="mw-headline" id=".E5.8F.82.E9.98.85">&#21442;&#38405;</span></h3>
<table class="t-dsc-begin"></table>
</div>
<div class="visualClear"></div>
</div>
</div>
</div>
</body>
</html>