Files
hustoj/web/cppreference-zh/chmhelp/cpp-io-basic_ios-operator!.html
2025-08-01 12:57:54 +08:00

184 lines
16 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::basic_ios&lt;CharT,Traits&gt;::operator!</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_io_basic_ios_operator 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::basic_ios&lt;CharT,Traits&gt;::</span>operator!</h1>
<div id="bodyContent">
<div id="contentSub"><span class="subpages">&lt; <a href="cpp.html">cpp</a>&lrm; | <a href="cpp-io.html">io</a>&lrm; | <a href="cpp-io-basic_ios.html">basic ios</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-dcl">
<td class="t-dcl-nopad">
<div><span class="mw-geshi cpp source-cpp"><span class="kw4">bool</span> operator<span class="sy3">!</span><span class="br0">(</span><span class="br0">)</span> <span class="kw4">const</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>&#33509;&#20851;&#32852;&#27969;&#19978;&#24050;&#20986;&#29616;&#38169;&#35823;&#21017;&#36820;&#22238; <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw2">true</span></span></span> &#12290;&#29305;&#21035;&#26159;&#33509; <span class="t-c"><span class="mw-geshi cpp source-cpp">rdstate<span class="br0">(</span><span class="br0">)</span></span></span> &#20013;&#35774;&#32622;&#20102; <code>failbit</code> &#25110; <code>badbit</code> &#21017;&#36820;&#22238; <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw2">true</span></span></span> &#12290;</p>
<h3><span class="mw-headline" id=".E5.8F.82.E6.95.B0">&#21442;&#25968;</span></h3>
<p>&#65288;&#26080;&#65289;</p>
<h3><span class="mw-headline" id=".E8.BF.94.E5.9B.9E.E5.80.BC">&#36820;&#22238;&#20540;</span></h3>
<p>&#33509;&#24050;&#20986;&#29616;&#38169;&#35823;&#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=".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="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
<a href="cpp-io-basic_ifstream.html"><span class="kw1732">std::<span class="me2">ifstream</span></span></a> file<span class="br0">(</span><span class="st0">&quot;test.txt&quot;</span><span class="br0">)</span><span class="sy4">;</span>
<span class="kw1">if</span><span class="br0">(</span><span class="sy3">!</span>file<span class="br0">)</span> <span class="co1">// operator! &#29992;&#20110;&#27492;</span>
<span class="br0">{</span>
<a href="cpp-io-cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> <span class="st0">&quot;File opening failed<span class="es1">\n</span>&quot;</span><span class="sy4">;</span>
<span class="kw1">return</span> <a href="cpp-utility-program-EXIT_status.html"><span class="kw973">EXIT_FAILURE</span></a><span class="sy4">;</span>
<span class="br0">}</span>
<span class="co1">// &#20856;&#22411;&#30340; C++ I/O &#24490;&#29615;&#20197; I/O &#20989;&#25968;&#30340;&#36820;&#22238;&#20540;&#20026;&#24490;&#29615;&#25511;&#21046;&#26465;&#20214;&#65292;</span>
<span class="co1">// operator bool() &#29992;&#20110;&#27492;</span>
<span class="kw1">for</span><span class="br0">(</span><span class="kw4">int</span> n<span class="sy4">;</span> file <span class="sy1">&gt;&gt;</span> n<span class="sy4">;</span> <span class="br0">)</span> <span class="br0">{</span>
<a href="cpp-io-cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> n <span class="sy1">&lt;&lt;</span> <span class="st0">&#39; &#39;</span><span class="sy4">;</span>
<span class="br0">}</span>
<a href="cpp-io-cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> <span class="st0">&#39;<span class="es1">\n</span>&#39;</span><span class="sy4">;</span>
<span class="kw1">if</span> <span class="br0">(</span>file.<span class="me1">bad</span><span class="br0">(</span><span class="br0">)</span><span class="br0">)</span>
<a href="cpp-io-cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> <span class="st0">&quot;I/O error while reading<span class="es1">\n</span>&quot;</span><span class="sy4">;</span>
<span class="kw1">else</span> <span class="kw1">if</span> <span class="br0">(</span>file.<span class="me1">eof</span><span class="br0">(</span><span class="br0">)</span><span class="br0">)</span>
<a href="cpp-io-cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> <span class="st0">&quot;End of file reached successfully<span class="es1">\n</span>&quot;</span><span class="sy4">;</span>
<span class="kw1">else</span> <span class="kw1">if</span> <span class="br0">(</span>file.<span class="me1">fail</span><span class="br0">(</span><span class="br0">)</span><span class="br0">)</span>
<a href="cpp-io-cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> <span class="st0">&quot;Non-integer data encountered<span class="es1">\n</span>&quot;</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">&#21442;&#38405;</span></h3>
<p>&#19979;&#34920;&#26174;&#31034; <code>basic_ios</code> &#35775;&#38382;&#22120;&#65288; <a href="cpp-io-basic_ios-good.html"><tt>good()</tt></a>&#12289; <a href="cpp-io-basic_ios-fail.html"><tt>fail()</tt></a> &#31561;&#65289;&#23545;&#20110; <a href="cpp-io-ios_base-iostate.html"><tt>ios_base::iostate</tt></a> &#26631;&#24535;&#30340;&#25152;&#26377;&#21487;&#33021;&#32452;&#21512;&#30340;&#20540;&#65306;</p>
<table class="wikitable" style="font-size:85%; text-align:center;">
<tr>
<td colspan="3"><a href="cpp-io-ios_base-iostate.html"><tt>ios_base::iostate</tt></a> &#26631;&#24535;</td>
<td colspan="6"><code>basic_ios</code> &#35775;&#38382;&#22120;</td>
</tr>
<tr>
<td>eofbit</td>
<td>failbit</td>
<td>badbit</td>
<td><a href="cpp-io-basic_ios-good.html"><tt>good()</tt></a></td>
<td><a href="cpp-io-basic_ios-fail.html"><tt>fail()</tt></a></td>
<td><a href="cpp-io-basic_ios-bad.html"><tt>bad()</tt></a></td>
<td><a href="cpp-io-basic_ios-eof.html"><tt>eof()</tt></a></td>
<td><a href="cpp-io-basic_ios-operator_bool.html"><tt>operator bool</tt></a></td>
<td><strong class="selflink"><tt>operator!</tt></strong></td>
</tr>
<tr>
<td style="background:#ff9090; color:black; vertical-align: middle; text-align: center;" class="table-no">false</td>
<td style="background:#ff9090; color:black; vertical-align: middle; text-align: center;" class="table-no">false</td>
<td style="background:#ff9090; color:black; vertical-align: middle; text-align: center;" class="table-no">false</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">true</td>
<td style="background:#ff9090; color:black; vertical-align: middle; text-align: center;" class="table-no">false</td>
<td style="background:#ff9090; color:black; vertical-align: middle; text-align: center;" class="table-no">false</td>
<td style="background:#ff9090; color:black; vertical-align: middle; text-align: center;" class="table-no">false</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">true</td>
<td style="background:#ff9090; color:black; vertical-align: middle; text-align: center;" class="table-no">false</td>
</tr>
<tr>
<td style="background:#ff9090; color:black; vertical-align: middle; text-align: center;" class="table-no">false</td>
<td style="background:#ff9090; color:black; vertical-align: middle; text-align: center;" class="table-no">false</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">true</td>
<td style="background:#ff9090; color:black; vertical-align: middle; text-align: center;" class="table-no">false</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">true</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">true</td>
<td style="background:#ff9090; color:black; vertical-align: middle; text-align: center;" class="table-no">false</td>
<td style="background:#ff9090; color:black; vertical-align: middle; text-align: center;" class="table-no">false</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">true</td>
</tr>
<tr>
<td style="background:#ff9090; color:black; vertical-align: middle; text-align: center;" class="table-no">false</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">true</td>
<td style="background:#ff9090; color:black; vertical-align: middle; text-align: center;" class="table-no">false</td>
<td style="background:#ff9090; color:black; vertical-align: middle; text-align: center;" class="table-no">false</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">true</td>
<td style="background:#ff9090; color:black; vertical-align: middle; text-align: center;" class="table-no">false</td>
<td style="background:#ff9090; color:black; vertical-align: middle; text-align: center;" class="table-no">false</td>
<td style="background:#ff9090; color:black; vertical-align: middle; text-align: center;" class="table-no">false</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">true</td>
</tr>
<tr>
<td style="background:#ff9090; color:black; vertical-align: middle; text-align: center;" class="table-no">false</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">true</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">true</td>
<td style="background:#ff9090; color:black; vertical-align: middle; text-align: center;" class="table-no">false</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">true</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">true</td>
<td style="background:#ff9090; color:black; vertical-align: middle; text-align: center;" class="table-no">false</td>
<td style="background:#ff9090; color:black; vertical-align: middle; text-align: center;" class="table-no">false</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">true</td>
</tr>
<tr>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">true</td>
<td style="background:#ff9090; color:black; vertical-align: middle; text-align: center;" class="table-no">false</td>
<td style="background:#ff9090; color:black; vertical-align: middle; text-align: center;" class="table-no">false</td>
<td style="background:#ff9090; color:black; vertical-align: middle; text-align: center;" class="table-no">false</td>
<td style="background:#ff9090; color:black; vertical-align: middle; text-align: center;" class="table-no">false</td>
<td style="background:#ff9090; color:black; vertical-align: middle; text-align: center;" class="table-no">false</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">true</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">true</td>
<td style="background:#ff9090; color:black; vertical-align: middle; text-align: center;" class="table-no">false</td>
</tr>
<tr>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">true</td>
<td style="background:#ff9090; color:black; vertical-align: middle; text-align: center;" class="table-no">false</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">true</td>
<td style="background:#ff9090; color:black; vertical-align: middle; text-align: center;" class="table-no">false</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">true</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">true</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">true</td>
<td style="background:#ff9090; color:black; vertical-align: middle; text-align: center;" class="table-no">false</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">true</td>
</tr>
<tr>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">true</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">true</td>
<td style="background:#ff9090; color:black; vertical-align: middle; text-align: center;" class="table-no">false</td>
<td style="background:#ff9090; color:black; vertical-align: middle; text-align: center;" class="table-no">false</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">true</td>
<td style="background:#ff9090; color:black; vertical-align: middle; text-align: center;" class="table-no">false</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">true</td>
<td style="background:#ff9090; color:black; vertical-align: middle; text-align: center;" class="table-no">false</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">true</td>
</tr>
<tr>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">true</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">true</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">true</td>
<td style="background:#ff9090; color:black; vertical-align: middle; text-align: center;" class="table-no">false</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">true</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">true</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">true</td>
<td style="background:#ff9090; color:black; vertical-align: middle; text-align: center;" class="table-no">false</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">true</td>
</tr>
</table>
</div>
<div class="visualClear"></div>
</div>
</div>
</div>
</body>
</html>