87 lines
5.7 KiB
HTML
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">< <a href="cpp.html">cpp</a>‎ | <a href="cpp-utility.html">utility</a>‎ | <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>定义于头文件 <code><a href="cpp-header-cstdlib.html"><cstdlib></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>以参数 <code>command</code> 调用运行环境的命令处理器(例如 <code>/bin/sh</code> 、 <code>cmd.exe</code> 、 <code>command.com</code> )。返回相应实现的定义值(通常是被调用程序所返回的值)。</p>
|
|
<p>若 <code>command</code> 为空,则检查运行环境是否有命令处理器,并当且仅当命令行存在才返回非零。</p>
|
|
<h3><span class="mw-headline" id=".E5.8F.82.E6.95.B0">参数</span></h3>
|
|
<table class="t-par-begin">
|
|
<tr class="t-par">
|
|
<td>command</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>实现定义值。若 <code>command</code> 为空指针,则当且仅当命令处理器存在才返回非零值。</p>
|
|
<h3><span class="mw-headline" id=".E6.B3.A8.E6.84.8F">注意</span></h3>
|
|
<p>POSIX 系统上,可用 <a rel="nofollow" class="external text" href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/wait.html">WEXITSTATUS 和 WSTOPSIG</a> 分解返回值。</p>
|
|
<p>相关的 POSIX 函数 <a rel="nofollow" class="external text" href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/popen.html">popen</a> 使调用方可获取到 <code>command</code> 生成的输出。</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 <cstdlib></span>
|
|
<span class="co2">#include <fstream></span>
|
|
<span class="co2">#include <iostream></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">"ls -l >test.txt"</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 执行 UNIX 命令 "ls -l >test.txt"</span>
|
|
<a href="cpp-io-cout.html"><span class="kw1757">std::<span class="me2">cout</span></span></a> <span class="sy1"><<</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">"test.txt"</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>可能的输出:</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">参阅</span></h3>
|
|
<table class="t-dsc-begin"></table>
|
|
</div>
|
|
<div class="visualClear"></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html>
|