<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[end of file, wenn der Stream Exceptions wirft]]></title><description><![CDATA[<pre><code class="language-cpp">while( !stream.eof() ) {
			char x = stream.get();
			cout  &lt;&lt;  x;
		}
</code></pre>
<p>Folgender Code wirft eine Exception beim Lesen über das letzte Zeichen hinaus. Leider sind fast alle Beispiele darauf ausgelegt, dass der Stream keine Exception wirft und die nehmen dann einfach das letzte Zeichen nicht sobald eof() true ergibt. Wie ist der elegante C++-Weg, wenn der Stream auf Exceptions eingestellt ist?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/122624/end-of-file-wenn-der-stream-exceptions-wirft</link><generator>RSS for Node</generator><lastBuildDate>Sun, 23 Aug 2026 06:34:19 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/122624.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 07 Oct 2005 18:40:39 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to end of file, wenn der Stream Exceptions wirft on Fri, 07 Oct 2005 18:40:39 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">while( !stream.eof() ) {
			char x = stream.get();
			cout  &lt;&lt;  x;
		}
</code></pre>
<p>Folgender Code wirft eine Exception beim Lesen über das letzte Zeichen hinaus. Leider sind fast alle Beispiele darauf ausgelegt, dass der Stream keine Exception wirft und die nehmen dann einfach das letzte Zeichen nicht sobald eof() true ergibt. Wie ist der elegante C++-Weg, wenn der Stream auf Exceptions eingestellt ist?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/887334</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/887334</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Fri, 07 Oct 2005 18:40:39 GMT</pubDate></item><item><title><![CDATA[Reply to end of file, wenn der Stream Exceptions wirft on Fri, 07 Oct 2005 18:43:44 GMT]]></title><description><![CDATA[<p>hilft das: <a href="http://fara.cs.uni-potsdam.de/~kaufmann/?page=GenCppFaqs&amp;faq=eof#Answ" rel="nofollow">http://fara.cs.uni-potsdam.de/~kaufmann/?page=GenCppFaqs&amp;faq=eof#Answ</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/887340</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/887340</guid><dc:creator><![CDATA[frage?]]></dc:creator><pubDate>Fri, 07 Oct 2005 18:43:44 GMT</pubDate></item><item><title><![CDATA[Reply to end of file, wenn der Stream Exceptions wirft on Fri, 07 Oct 2005 18:47:11 GMT]]></title><description><![CDATA[<p>thx</p>
]]></description><link>https://www.c-plusplus.net/forum/post/887345</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/887345</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Fri, 07 Oct 2005 18:47:11 GMT</pubDate></item><item><title><![CDATA[Reply to end of file, wenn der Stream Exceptions wirft on Fri, 07 Oct 2005 18:55:47 GMT]]></title><description><![CDATA[<p>Nein, hilft nicht, denn die Beschreibung dort verhindert, falls ich es richtig verstanden habe, nicht die Exception. Wahrscheinlich würde beim Fangen der Exception der Test auf eof() dann passen, aber ich will keine Exception für EOF haben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/887350</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/887350</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Fri, 07 Oct 2005 18:55:47 GMT</pubDate></item><item><title><![CDATA[Reply to end of file, wenn der Stream Exceptions wirft on Fri, 07 Oct 2005 19:52:14 GMT]]></title><description><![CDATA[<p>Du kannst dem Stream sagen, bei welchen (Fehler-)Zuständen er exceptions werfen soll:<br />
Du brauchst dementsprechend:</p>
<pre><code class="language-cpp">strm.exceptions (std::ios::failbit | std::ios::badbit);
</code></pre>
<p>Dann sollten &quot;echte&quot; Fehler noch exceptions generieren, eof aber nicht.<br />
Den alten Zustand kannst Du Dir vorher mit strm.exceptions() merken und am Ende zurücksetzen.</p>
<p>Der entsprechende Text aus N. Josuttis &quot;C++ Standard Library, The: A Tutorial and Reference&quot;</p>
<blockquote>
<p>Calling exceptions() without an argument yields the current flags for which exceptions are triggered. No exceptions are thrown if the function returns goodbit. This is the default, to maintain backward compatibility. When exceptions() is called with an argument, exceptions are thrown as soon as the corresponding state flags are set. If a state flag is already set when exceptions() is called with an argument, an exceptions is thrown if the corresponding flag is set in the argument.</p>
<p>The following example configures the stream so that, for all flags, an exception is thrown:</p>
<p>// throw exceptions for all &quot;errors&quot;<br />
strm.exceptions (std::ios::eofbit | std::ios::failbit |<br />
std::ios::badbit);</p>
<p>If 0 or goodbit is passed as an argument, no exceptions are generated:</p>
<p>// do not generate exceptions<br />
strm.exceptions (std::ios::goodbit);</p>
<p>Exceptions are thrown when the corresponding state flags are set after calling clear() or setstate(). An exception is even thrown if the flag was already set and not cleared:</p>
<p>// this call throws an exception if failbit is set on entry<br />
strm.exceptions (std::ios::failbit);<br />
...<br />
// throw an exception (even if failbit was already set)<br />
strm.setstate (std::ios::failbit);</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/887378</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/887378</guid><dc:creator><![CDATA[niemand]]></dc:creator><pubDate>Fri, 07 Oct 2005 19:52:14 GMT</pubDate></item><item><title><![CDATA[Reply to end of file, wenn der Stream Exceptions wirft on Fri, 07 Oct 2005 21:05:55 GMT]]></title><description><![CDATA[<p>niemand schrieb:</p>
<blockquote>
<p>Du kannst dem Stream sagen, bei welchen (Fehler-)Zuständen er exceptions werfen soll:<br />
Du brauchst dementsprechend:</p>
<pre><code class="language-cpp">strm.exceptions (std::ios::failbit | std::ios::badbit);
</code></pre>
<p>Dann sollten &quot;echte&quot; Fehler noch exceptions generieren, eof aber nicht.<br />
Den alten Zustand kannst Du Dir vorher mit strm.exceptions() merken und am Ende zurücksetzen. [...]</p>
</blockquote>
<p>Wenn ich die eof-Exception ganz abstelle, krieg ich sie auch nicht, wenn ich 20 Bytes über das Dateiende hinauslese und da möchte ich sie schon ganz gerne haben. Also ist abstellen für mich keine Option.<br />
Also letztlich möchte ich meine Frage nochmal präzisieren, kann man das Dateiende feststellen, ohne darüber hinauszulesen? Und ich vermute schon die Antwort... <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /><br />
Ok, muss ich wohl akzeptieren.</p>
<p>thx</p>
]]></description><link>https://www.c-plusplus.net/forum/post/887423</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/887423</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Fri, 07 Oct 2005 21:05:55 GMT</pubDate></item><item><title><![CDATA[Reply to end of file, wenn der Stream Exceptions wirft on Fri, 07 Oct 2005 21:34:17 GMT]]></title><description><![CDATA[<p>Wirft auch peek? Ich würde es für logisch halten wenn.</p>
<pre><code class="language-cpp">while(in.peek() != EOF)
</code></pre>
<p>nicht werfen würde jedoch bei den iostreams weiß man nie.</p>
<p>Mein ganz persöhnlicher Rat und Meinung ist es diese Streams einfach in die Tonne zu treten.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/887446</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/887446</guid><dc:creator><![CDATA[Ben04]]></dc:creator><pubDate>Fri, 07 Oct 2005 21:34:17 GMT</pubDate></item><item><title><![CDATA[Reply to end of file, wenn der Stream Exceptions wirft on Fri, 07 Oct 2005 21:52:54 GMT]]></title><description><![CDATA[<p>Die Option, sie in die Tonne zu treten, habe ich leider auch nicht, weil ich das für die Uni mache. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /> Aber ich kann scheinbar bis zum EOF seeken. Ich werde Morgen mal schaun, ob mir das nützlich ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/887459</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/887459</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Fri, 07 Oct 2005 21:52:54 GMT</pubDate></item></channel></rss>