The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
 lang="en" dir="ltr">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>
    eof    [C++ Reference]
  </title>

  <meta name="generator" content="DokuWiki Release 2009-12-25c &quot;Lemming&quot;" />
<meta name="robots" content="index,follow" />
<meta name="date" content="2009-03-09T12:14:48-0700" />
<meta name="keywords" content="io,eof" />
<link rel="search" type="application/opensearchdescription+xml" href="/wiki/lib/exe/opensearch.php" title="C++ Reference" />
<link rel="start" href="/wiki/" />
<link rel="contents" href="/wiki/io/eof?do=index" title="Index" />
<link rel="alternate" type="application/rss+xml" title="Recent Changes" href="/wiki/feed.php" />
<link rel="alternate" type="application/rss+xml" title="Current Namespace" href="/wiki/feed.php?mode=list&amp;ns=io" />
<link rel="edit" title="Edit this page" href="/wiki/io/eof?do=edit" />
<link rel="alternate" type="text/html" title="Plain HTML" href="/wiki/_export/xhtml/io/eof" />
<link rel="alternate" type="text/plain" title="Wiki Markup" href="/wiki/_export/raw/io/eof" />
<link rel="canonical" href="http://www.cppreference.com/wiki/io/eof" />
<link rel="stylesheet" media="all" type="text/css" href="/wiki/lib/exe/css.php?s=all&amp;t=custom1&amp;tseed=1272971091" />
<link rel="stylesheet" media="screen" type="text/css" href="/wiki/lib/exe/css.php?t=custom1&amp;tseed=1272971091" />
<link rel="stylesheet" media="print" type="text/css" href="/wiki/lib/exe/css.php?s=print&amp;t=custom1&amp;tseed=1272971091" />
<script type="text/javascript" charset="utf-8" ><!--//--><![CDATA[//><!--
var NS='io';var JSINFO = {"id":"io:eof","namespace":"io"};
//--><!]]></script>
<script type="text/javascript" charset="utf-8" src="/wiki/lib/exe/js.php?tseed=1272971091" ></script>

  <link rel="shortcut icon" href="/wiki/lib/tpl/custom1/images/favicon.png" />

  </head>

<body>
<div class="dokuwiki">
  
  <div class="stylehead">

    <div class="breadcrumbs">
      <span class="bchead">You are here: </span><a href="../start.html"  title="start">C++ Reference</a> &raquo; <a href="../io/start.html"  title="io:start">C++ I/O</a> &raquo; <a href="../io/eof.html"  title="io:eof">eof</a>    </div>
    
  </div>


  
  
  <div class="page">

    <script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2828341-1";
urchinTracker();
</script>
    <!-- wikipage start -->
    


<h2><a name="eof" id="eof">eof</a></h2>
<div class="level2">

<p>
Syntax:
</p>
<pre class="c code c++" style="font-family:monospace;">    bool istream<span class="sy0">::</span><span class="me2">eof</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre>
<p>
The function eof() returns true if the end of the associated input file has been reached, false otherwise.
</p>

<p>
A stream goes into <acronym title="End of file">EOF</acronym> state whenever the end of stream is seen, i.e. a character past the end has been read. As operator» and <a href="../io/getline.html" class="wikilink1" title="io:getline">getline</a> normally keep reading characters until the end of token (until whitespace, invalid characters, line terminator or <acronym title="End of file">EOF</acronym>) it is possible that the stream <acronym title="End of file">EOF</acronym> flag gets set even though the token was read correctly. Conversely, the stream does not go into <acronym title="End of file">EOF</acronym> state if there happens to be any whitespace after the last token, but trying to read another token will still fail.
</p>

<p>
Therefore, the <acronym title="End of file">EOF</acronym> flag <strong>cannot</strong> be used as a test in a loop intended to read all stream contents until <acronym title="End of file">EOF</acronym>.
</p>

<p>
Instead, one should check for the fail condition after an attempt to read. This is done most conveniently by testing the stream itself, as follows:
</p>
<pre class="c code c++" style="font-family:monospace;">   std<span class="sy0">::</span><span class="me2">ifstream</span> file<span class="br0">&#40;</span><span class="st0">&quot;test.txt&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
   std<span class="sy0">::</span><span class="kw4">string</span> line<span class="sy0">;</span>
   <span class="kw1">while</span> <span class="br0">&#40;</span>std<span class="sy0">::</span><span class="me2">getline</span><span class="br0">&#40;</span>file<span class="sy0">,</span> line<span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
     <span class="co1">// A line was read successfully, so you can process it</span>
   <span class="br0">&#125;</span></pre>
<p>
Line 7 of the example below illustrates the main use for checking the <acronym title="End of file">EOF</acronym> state: after a failed read.   In such a situation, it can be used to determine whether or not the fail was caused by reaching the end of stream.
</p>
<pre class="c code c++" style="font-family:monospace;"><span class="lno">1:</span>    std<span class="sy0">::</span><span class="me2">ifstream</span> file<span class="br0">&#40;</span><span class="st0">&quot;test.txt&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="lno">2:</span>    std<span class="sy0">::</span><span class="kw4">string</span> word<span class="sy0">;</span>
<span class="lno">3:</span>    <span class="kw4">double</span> value<span class="sy0">;</span>
<span class="lno">4:</span>    <span class="kw1">while</span> <span class="br0">&#40;</span>file <span class="sy0">&gt;&gt;</span> word <span class="sy0">&gt;&gt;</span> value<span class="br0">&#41;</span> <span class="br0">&#123;</span>
<span class="lno">5:</span>      <span class="co1">// A word and a double value were both read successfully</span>
<span class="lno">6:</span>    <span class="br0">&#125;</span>
<span class="lno">7:</span>    <span class="kw1">if</span> <span class="br0">&#40;</span><span class="sy0">!</span>file.<span class="me1">eof</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> throw std<span class="sy0">::</span><span class="me2">runtime_error</span><span class="br0">&#40;</span><span class="st0">&quot;Invalid data from file&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
</pre>
<p>
The table below lists a number of different states that a stream may be in:

</p>
<table class="inline">
	<tr class="row0">
		<th class="col0">Test</th><th class="col1">Description</th>
	</tr>
	<tr class="row1">
		<td class="col0">if (s)</td><td class="col1">The previous operation was successful (a shorthand for !s.fail()).</td>
	</tr>
	<tr class="row2">
		<td class="col0">if (s.fail())</td><td class="col1">The previous operation failed.</td>
	</tr>
	<tr class="row3">
		<td class="col0">if (s.eof())</td><td class="col1">Reading past the end has been attempted.</td>
	</tr>
	<tr class="row4">
		<td class="col0">if (s.bad())</td><td class="col1">Stream state is undefined; the stream can no longer be used.</td>
	</tr>
	<tr class="row5">
		<td class="col0">if (s.good())</td><td class="col1">None of bad/eof/fail are set.</td>
	</tr>
</table>

<p>

Related Topics: <a href="../io/bad.html" class="wikilink1" title="io:bad">bad</a>, <a href="../io/clear.html" class="wikilink1" title="io:clear">clear</a>, <a href="../io/exceptions.html" class="wikilink1" title="io:exceptions">exceptions</a>, <a href="../io/fail.html" class="wikilink1" title="io:fail">fail</a>, <a href="../io/good.html" class="wikilink1" title="io:good">good</a>, <a href="../io/rdstate.html" class="wikilink1" title="io:rdstate">rdstate</a>
</p>

</div>

    <!-- wikipage stop -->
  </div>

  <div class="clearer">&nbsp;</div>

  
  <div class="stylefoot">

    <div class="meta">
      <div class="user">
              </div>
      <!--
      <div class="doc">
        io/eof.txt &middot; Last modified: 03/09/2009 12:14 by 164.76.9.87      </div>
      -->
    </div>

   
    </div></div></body>
</html>