<?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[String bis Zeilenende per Stream einlesen]]></title><description><![CDATA[<p>Hoi,</p>
<p>ich habe ein Format der Art</p>
<blockquote>
<p>&lt;Zahl&gt; &lt;Zahl&gt; &lt;Zahl&gt; [Optionaler String] *Zeilenende*</p>
</blockquote>
<p>Zwischen der letzten Zahl und dem String sind beliebig viele Leerzeichen/Tabulatoren.<br />
Meine Lösung ist getline() und den String trimmen.</p>
<p>Geht das eleganter, also ohne das trimmen?</p>
<p>Danke und Grüße,<br />
Ethon</p>
<p>Edit: Ich weiß, ich könnte mit einer manuellen Schleifen Spacces überspringen und dann getline aufrufen. Da es hierbei aber um das Raushängen des C++-Penis geht suche ich die eleganteste Streamlösung. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/topic/318083/string-bis-zeilenende-per-stream-einlesen</link><generator>RSS for Node</generator><lastBuildDate>Mon, 27 Jul 2026 15:29:23 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/318083.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 30 Jun 2013 12:37:30 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to String bis Zeilenende per Stream einlesen on Sun, 30 Jun 2013 12:45:24 GMT]]></title><description><![CDATA[<p>Hoi,</p>
<p>ich habe ein Format der Art</p>
<blockquote>
<p>&lt;Zahl&gt; &lt;Zahl&gt; &lt;Zahl&gt; [Optionaler String] *Zeilenende*</p>
</blockquote>
<p>Zwischen der letzten Zahl und dem String sind beliebig viele Leerzeichen/Tabulatoren.<br />
Meine Lösung ist getline() und den String trimmen.</p>
<p>Geht das eleganter, also ohne das trimmen?</p>
<p>Danke und Grüße,<br />
Ethon</p>
<p>Edit: Ich weiß, ich könnte mit einer manuellen Schleifen Spacces überspringen und dann getline aufrufen. Da es hierbei aber um das Raushängen des C++-Penis geht suche ich die eleganteste Streamlösung. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2335427</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2335427</guid><dc:creator><![CDATA[Ethon]]></dc:creator><pubDate>Sun, 30 Jun 2013 12:45:24 GMT</pubDate></item><item><title><![CDATA[Reply to String bis Zeilenende per Stream einlesen on Sun, 30 Jun 2013 12:50:32 GMT]]></title><description><![CDATA[<p>Einfach int lesen, int lesen, int lesen,<br />
Whitespace skippen (Manipulator std::ws),<br />
prüfen ob das nächste Zeichen '\n' ist, wenn ja fertig, wenn nein string lesen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2335432</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2335432</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Sun, 30 Jun 2013 12:50:32 GMT</pubDate></item><item><title><![CDATA[Reply to String bis Zeilenende per Stream einlesen on Sun, 30 Jun 2013 12:55:24 GMT]]></title><description><![CDATA[<p>Nur dass '\n' als Whitespace gilt und auch gefiltert wird.<br />
Dh. ich muss die locale ändern.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2335435</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2335435</guid><dc:creator><![CDATA[Ethon]]></dc:creator><pubDate>Sun, 30 Jun 2013 12:55:24 GMT</pubDate></item><item><title><![CDATA[Reply to String bis Zeilenende per Stream einlesen on Sun, 30 Jun 2013 12:59:55 GMT]]></title><description><![CDATA[<p>Edit³: Ok, habs. Das ist das Problem... <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f4a1.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--light_bulb"
      title=":bulb:"
      alt="💡"
    /> s</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2335437</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2335437</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Sun, 30 Jun 2013 12:59:55 GMT</pubDate></item><item><title><![CDATA[Reply to String bis Zeilenende per Stream einlesen on Sun, 30 Jun 2013 13:11:44 GMT]]></title><description><![CDATA[<p>C++ und Penis in einem Satz! Der Erfinden von C++ würde sich im Grab umdrehen, wenn er sowas lesen würde.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2335445</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2335445</guid><dc:creator><![CDATA[clang]]></dc:creator><pubDate>Sun, 30 Jun 2013 13:11:44 GMT</pubDate></item><item><title><![CDATA[Reply to String bis Zeilenende per Stream einlesen on Sun, 30 Jun 2013 13:18:40 GMT]]></title><description><![CDATA[<p>Ethon schrieb:</p>
<blockquote>
<p>Nur dass '\n' als Whitespace gilt und auch gefiltert wird.<br />
Dh. ich muss die locale ändern.</p>
</blockquote>
<p>Oh, stimmt ja.<br />
Mein Fheler.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2335451</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2335451</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Sun, 30 Jun 2013 13:18:40 GMT</pubDate></item><item><title><![CDATA[Reply to String bis Zeilenende per Stream einlesen on Sun, 30 Jun 2013 13:54:41 GMT]]></title><description><![CDATA[<p>clang schrieb:</p>
<blockquote>
<p>C++ und Penis in einem Satz! Der Erfinden von C++ würde sich im Grab umdrehen, wenn er sowas lesen würde.</p>
</blockquote>
<p>Zum Glück lebt er noch ...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2335469</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2335469</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Sun, 30 Jun 2013 13:54:41 GMT</pubDate></item><item><title><![CDATA[Reply to String bis Zeilenende per Stream einlesen on Mon, 01 Jul 2013 19:55:49 GMT]]></title><description><![CDATA[<p>Hi Ethon,</p>
<p>wenn es hier um 'ne C++-raushängende Lösung geht, wie wäre es dann mit einer gettrimmedline-Funktion - als Pendant zu std::getline:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;locale&gt; // std::ctype

#include &lt;sstream&gt; // std::istringstream (s.main)

template&lt; typename E, typename Traits &gt;
std::basic_istream&lt; E, Traits &gt;&amp; gettrimmedline( std::basic_istream&lt; E, Traits &gt;&amp; in, std::basic_string&lt; E, Traits &gt;&amp; str )
{
    std::basic_istream&lt; E, Traits &gt;::sentry ok( in, true ); // true := noskipws
    if( ok )
    {
        std::ios_base::iostate state = std::ios_base::goodbit;
        try
        {
            bool readsome = false;
            std::basic_string&lt; E, Traits &gt;::size_type len = 0;
            str.clear();
            const std::ctype&lt; E &gt;&amp; ctype_ = std::use_facet&lt; std::ctype&lt; E &gt; &gt;( in.getloc() );
            const E delim = ctype_.widen('\n');
            for(;;)
            {
                const Traits::int_type m = in.rdbuf()-&gt;sbumpc();
                if( Traits::eq_int_type( m, Traits::eof() ) )
                {
                    state |= std::ios_base::eofbit;
                    break;
                }
                const E c = Traits::to_char_type( m );
                readsome = true;
                if( Traits::eq( c, delim ) ) // Zeilenende
                    break;
                if( ctype_.is( std::ctype_base::space, c ) )
                {
                    if( str.empty() )
                        continue; // überliest führende white character
                }
                else
                    len = str.length()+1; // Länge merken mit dem letzten 'not white character'
                str.append( 1, c );
            }
            if( readsome )
                str.resize( len ); // ggf. white character am Ende entfernen
            else
                state |= std::ios_base::failbit;
        }
        catch( ... )
        {
            state |= std::ios_base::badbit;
            if( in.exceptions() &amp; std::ios_base::badbit )
                throw;
        }
        in.setstate( state );
    }
    return in;
}

int main()
{
    using namespace std;
    istringstream in(
        &quot;1 23 45 das ist ein Text        \n&quot;
        &quot;1 67 89\n&quot;
        &quot;1 -9 23     \n&quot;
        &quot;1 17 89&quot;  // Bem.: 'Leerer' Inhalt führt zu Fehler
        );
    int a, b, c;
    for( string s; gettrimmedline( in &gt;&gt; a &gt;&gt; b &gt;&gt; c, s ); )
        cout &lt;&lt; &quot;Optionaler Text: [&quot; &lt;&lt; s &lt;&lt; &quot;]&quot; &lt;&lt; endl;
    return 0;
}
</code></pre>
<p>Gruß<br />
Werner</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2335817</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2335817</guid><dc:creator><![CDATA[Werner Salomon]]></dc:creator><pubDate>Mon, 01 Jul 2013 19:55:49 GMT</pubDate></item></channel></rss>