<?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[Stream: mehrere Zahlen]]></title><description><![CDATA[<p>Hi,</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;string&gt;

using namespace std;

struct Subtest
{
	int x;

	friend ostream&amp; operator&lt;&lt;(ostream&amp; os, const Subtest&amp; subtest)
	{
		return os &lt;&lt; subtest.x;
	}

	friend istream&amp; operator&gt;&gt;(istream&amp; is, Subtest&amp; subtest)
	{
		return is &gt;&gt; subtest.x;
	}
};

struct Test
{
	int a;
	Subtest b;

	friend ostream&amp; operator&lt;&lt;(ostream&amp; os, const Test&amp; test)
	{
		return os &lt;&lt; test.a &lt;&lt; test.b;
	}

	friend istream&amp; operator&gt;&gt;(istream&amp; is, Test&amp; test)
	{
		return is &gt;&gt; test.a &gt;&gt; test.b;
	}
};

int main()
{
	Test x;
	x.a = 100;
	x.b.x = 200;

	cout &lt;&lt; x;

	Test y;
	{
		stringstream ss;
		ss &lt;&lt; x;

		ss &gt;&gt; y;
	}

	cout &lt;&lt; '\n' &lt;&lt; y;

	int z;
	cin &gt;&gt; z;
}
</code></pre>
<p>Dieses sehr simple Beispiel gibt mir in Zeile 2 100200-12347891 aus. Klar ist, dass er also 100200 als eine Zahl interpretiert. Die offensichtliche Lösung ist ein Leerzeichen zwischen die Zahlen zu stecken. Bisher habe ich in Serializer-Lösungen über Streams so etwas aber nicht gesehen. Ist es legitim das so zu machen oder gibt es bessere Varianten?</p>
<p>Ich frage deshalb, weil dieses Problem ja nicht so einfach zu lösen ist, wenn man ein beliebiges Element, etwa über Templates, serialisieren möchte.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/309069/stream-mehrere-zahlen</link><generator>RSS for Node</generator><lastBuildDate>Wed, 05 Aug 2026 12:09:26 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/309069.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 13 Oct 2012 11:57:56 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Stream: mehrere Zahlen on Sat, 13 Oct 2012 12:02:29 GMT]]></title><description><![CDATA[<p>Hi,</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;string&gt;

using namespace std;

struct Subtest
{
	int x;

	friend ostream&amp; operator&lt;&lt;(ostream&amp; os, const Subtest&amp; subtest)
	{
		return os &lt;&lt; subtest.x;
	}

	friend istream&amp; operator&gt;&gt;(istream&amp; is, Subtest&amp; subtest)
	{
		return is &gt;&gt; subtest.x;
	}
};

struct Test
{
	int a;
	Subtest b;

	friend ostream&amp; operator&lt;&lt;(ostream&amp; os, const Test&amp; test)
	{
		return os &lt;&lt; test.a &lt;&lt; test.b;
	}

	friend istream&amp; operator&gt;&gt;(istream&amp; is, Test&amp; test)
	{
		return is &gt;&gt; test.a &gt;&gt; test.b;
	}
};

int main()
{
	Test x;
	x.a = 100;
	x.b.x = 200;

	cout &lt;&lt; x;

	Test y;
	{
		stringstream ss;
		ss &lt;&lt; x;

		ss &gt;&gt; y;
	}

	cout &lt;&lt; '\n' &lt;&lt; y;

	int z;
	cin &gt;&gt; z;
}
</code></pre>
<p>Dieses sehr simple Beispiel gibt mir in Zeile 2 100200-12347891 aus. Klar ist, dass er also 100200 als eine Zahl interpretiert. Die offensichtliche Lösung ist ein Leerzeichen zwischen die Zahlen zu stecken. Bisher habe ich in Serializer-Lösungen über Streams so etwas aber nicht gesehen. Ist es legitim das so zu machen oder gibt es bessere Varianten?</p>
<p>Ich frage deshalb, weil dieses Problem ja nicht so einfach zu lösen ist, wenn man ein beliebiges Element, etwa über Templates, serialisieren möchte.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2259932</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2259932</guid><dc:creator><![CDATA[Eisflamme]]></dc:creator><pubDate>Sat, 13 Oct 2012 12:02:29 GMT</pubDate></item><item><title><![CDATA[Reply to Stream: mehrere Zahlen on Sat, 13 Oct 2012 13:11:30 GMT]]></title><description><![CDATA[<p>Stream im Binärmodus öffnen und Typen fester Breite reinschieben?</p>
<p>// edit:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;cstdint&gt;

class foo_t {

	friend std::ostream&amp; operator&lt;&lt;( std::ostream &amp;os, foo_t const &amp;foo );

	friend std::ofstream&amp; operator&lt;&lt;( std::ofstream &amp;os, foo_t const &amp;foo );
	friend std::ifstream&amp; operator&gt;&gt;( std::ifstream &amp;is, foo_t &amp;foo );

	private:
		int32_t bar;

	public:
		foo_t( ) : bar( 0 ) {}
		foo_t( int bar ) : bar( bar ) {}
};

std::ostream&amp; operator&lt;&lt;( std::ostream &amp;os, foo_t const &amp;foo )
{
	os &lt;&lt; foo.bar;
	return os;
}

std::ofstream&amp; operator&lt;&lt;( std::ofstream &amp;os, foo_t const &amp;foo )
{
	os.write( reinterpret_cast&lt; char const* &gt;( &amp;foo.bar ), sizeof( foo.bar ) );
	return os;
}

std::ifstream&amp; operator&gt;&gt;( std::ifstream &amp;is, foo_t &amp;foo )
{
	is.read( reinterpret_cast&lt; char * &gt;( &amp;foo.bar ), sizeof( foo.bar ) );
	return is;
}

int main()
{
	{
		foo_t a( 43 );
		foo_t b( 98 );

		std::ofstream file( &quot;data.dat&quot;, std::ios_base::binary | std::ios_base::trunc );
		file &lt;&lt; a;
		file &lt;&lt; b;
	}

	{
		foo_t a;
		foo_t b;

		std::ifstream file( &quot;data.dat&quot;, std::ios_base::binary );
		file &gt;&gt; a;
		file &gt;&gt; b;

		std::cout &lt;&lt; &quot;a = &quot; &lt;&lt; a &lt;&lt; &quot;\nb = &quot; &lt;&lt; b &lt;&lt; '\n';
	}
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2259948</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2259948</guid><dc:creator><![CDATA[Swordfish]]></dc:creator><pubDate>Sat, 13 Oct 2012 13:11:30 GMT</pubDate></item><item><title><![CDATA[Reply to Stream: mehrere Zahlen on Sat, 13 Oct 2012 14:16:50 GMT]]></title><description><![CDATA[<p>Okay, das wäre eine Lösung. Aber dann sind die operatoren&lt;&lt;/&gt;&gt; für cout und cin z.B. nicht mehr zu gebrauchen. Zudem ist dann alles voller reinterpret_casts. Das gibt mir irgendwie das Gefühl, dass die Lösung nicht besonders elegant ist, aber vielleicht gibt es auch keine andere.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2259986</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2259986</guid><dc:creator><![CDATA[Eisflamme]]></dc:creator><pubDate>Sat, 13 Oct 2012 14:16:50 GMT</pubDate></item><item><title><![CDATA[Reply to Stream: mehrere Zahlen on Sat, 13 Oct 2012 14:57:45 GMT]]></title><description><![CDATA[<p>Eisflamme schrieb:</p>
<blockquote>
<p>Okay, das wäre eine Lösung. Aber dann sind die operatoren&lt;&lt;/&gt;&gt; für cout und cin z.B. nicht mehr zu gebrauchen.</p>
</blockquote>
<p>Warum nicht?</p>
<p>Eisflamme schrieb:</p>
<blockquote>
<p>Zudem ist dann alles voller reinterpret_casts.</p>
</blockquote>
<p>Wenn's angebracht ist, ist am <code>reinterpret_cast</code> nichts schlimmes.</p>
<p>Eisflamme schrieb:</p>
<blockquote>
<p>Das gibt mir irgendwie das Gefühl, dass die Lösung nicht besonders elegant ist, aber vielleicht gibt es auch keine andere.</p>
</blockquote>
<p>Sonst gingen im Textformat noch Delimiter, feste Breite oder variable Breite mit Angabe der Länge. Letztere beide mMn. bescheiden.</p>
<p>Vielleicht hat jemand noch einen anderen Vorschlag?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2260002</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2260002</guid><dc:creator><![CDATA[Swordfish]]></dc:creator><pubDate>Sat, 13 Oct 2012 14:57:45 GMT</pubDate></item><item><title><![CDATA[Reply to Stream: mehrere Zahlen on Sat, 13 Oct 2012 15:03:56 GMT]]></title><description><![CDATA[<p>es geht um das einlesen von zahlen und ihr macht da so ein affentheater draus <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f644.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_rolling_eyes"
      title=":rolling_eyes:"
      alt="🙄"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2260003</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2260003</guid><dc:creator><![CDATA[kasper]]></dc:creator><pubDate>Sat, 13 Oct 2012 15:03:56 GMT</pubDate></item><item><title><![CDATA[Reply to Stream: mehrere Zahlen on Sat, 13 Oct 2012 15:09:23 GMT]]></title><description><![CDATA[<p>Swordfisch:<br />
Weil der mir die Zahlen binär ausgibt und ich bei der cin-Eingabe auch nicht spontan auf das Zeichen für 100 oder 200 komme. Ich will die aber eigentlich ganz gerne sehen. <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="😉"
    /> Ja, die von Dir erwähnten Alternativen gefallen mir auch weniger gut. Ein ' ' hinzuzufügen empfinde ich gerade jedoch noch etwas weniger unelegant.</p>
<p>kasper:<br />
Na und? Wie würdest Du es denn lösen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2260005</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2260005</guid><dc:creator><![CDATA[Eisflamme]]></dc:creator><pubDate>Sat, 13 Oct 2012 15:09:23 GMT</pubDate></item><item><title><![CDATA[Reply to Stream: mehrere Zahlen on Sat, 13 Oct 2012 15:17:12 GMT]]></title><description><![CDATA[<p>Eisflamme schrieb:</p>
<blockquote>
<p>kasper:<br />
Na und? Wie würdest Du es denn lösen?</p>
</blockquote>
<p>na und weiter nichts, ist einfach nur low level sinnig. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /><br />
fscanf.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2260008</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2260008</guid><dc:creator><![CDATA[kasper]]></dc:creator><pubDate>Sat, 13 Oct 2012 15:17:12 GMT</pubDate></item><item><title><![CDATA[Reply to Stream: mehrere Zahlen on Sat, 13 Oct 2012 15:19:27 GMT]]></title><description><![CDATA[<p>Ne, will C++ nutzen. Außerdem ist das auch nicht besser als die obige Lösung.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2260010</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2260010</guid><dc:creator><![CDATA[Eisflamme]]></dc:creator><pubDate>Sat, 13 Oct 2012 15:19:27 GMT</pubDate></item><item><title><![CDATA[Reply to Stream: mehrere Zahlen on Sat, 13 Oct 2012 15:23:24 GMT]]></title><description><![CDATA[<p>Eisflamme schrieb:</p>
<blockquote>
<p>Ne, will C++ nutzen. Außerdem ist das auch nicht besser als die obige Lösung.</p>
</blockquote>
<p>wenn du schlankeren code und einfachere lesbarkeit nicht als besser ansiehst dann kann ich dir auch nicht weiter helfen.<br />
weiterhin viel spaß beim verkomplizieren der angelegenheit.<br />
- und tschüs -</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2260013</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2260013</guid><dc:creator><![CDATA[kasper]]></dc:creator><pubDate>Sat, 13 Oct 2012 15:23:24 GMT</pubDate></item><item><title><![CDATA[Reply to Stream: mehrere Zahlen on Sat, 13 Oct 2012 15:35:55 GMT]]></title><description><![CDATA[<p>Eisflamme schrieb:</p>
<blockquote>
<p>Weil der mir die Zahlen binär ausgibt und ich bei der cin-Eingabe auch nicht spontan auf das Zeichen für 100 oder 200 komme. Ich will die aber eigentlich ganz gerne sehen. <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>
</blockquote>
<p>Nur die Varianten für <code>ifstream</code> und <code>ofstream</code> schreiben byteweise.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2260020</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2260020</guid><dc:creator><![CDATA[Swordfish]]></dc:creator><pubDate>Sat, 13 Oct 2012 15:35:55 GMT</pubDate></item><item><title><![CDATA[Reply to Stream: mehrere Zahlen on Sat, 13 Oct 2012 20:30:20 GMT]]></title><description><![CDATA[<p>Swordfish:<br />
Stimmt, da habe ich nicht genau genug geschaut, dann ist das okay. So vieleiInterpret_castsblähen den Code halt ziemlich auf. Wie löst ein Werner z.B. dieses Problem?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2260133</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2260133</guid><dc:creator><![CDATA[Eisflamme]]></dc:creator><pubDate>Sat, 13 Oct 2012 20:30:20 GMT</pubDate></item><item><title><![CDATA[Reply to Stream: mehrere Zahlen on Sat, 13 Oct 2012 20:40:44 GMT]]></title><description><![CDATA[<p>Eisflamme schrieb:</p>
<blockquote>
<p>Wie löst ein Werner z.B. dieses Problem?</p>
</blockquote>
<p>Hallo Eisflamme,</p>
<p>ganz einfach mit einem Leerzeichen zwischen den Zahlen. Ich sehe da auch gar kein Problem; oder habe ich hier was überlesen?</p>
<pre><code class="language-cpp">struct Test
{
    int a;
    Subtest b;

    friend ostream&amp; operator&lt;&lt;(ostream&amp; os, const Test&amp; test)
    {
        return os &lt;&lt; test.a &lt;&lt; &quot; &quot; &lt;&lt; test.b; // Leerzeichen als Trenner
    } 
    // usw.
</code></pre>
<p>Eisflamme schrieb:</p>
<blockquote>
<p>Die offensichtliche Lösung ist ein Leerzeichen zwischen die Zahlen zu stecken. Bisher habe ich in Serializer-Lösungen über Streams so etwas aber nicht gesehen.</p>
</blockquote>
<p>ich schon, warum auch nicht</p>
<p>Gruß<br />
Werner</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2260137</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2260137</guid><dc:creator><![CDATA[Werner Salomon]]></dc:creator><pubDate>Sat, 13 Oct 2012 20:40:44 GMT</pubDate></item><item><title><![CDATA[Reply to Stream: mehrere Zahlen on Sat, 13 Oct 2012 21:25:14 GMT]]></title><description><![CDATA[<p>Okay, cool, so sieht meine aktuelle Lösung auch aus. <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="🙂"
    /> Ich muss dann nur aufpassen, wenn beim Serialisieren plötzlich Strings auftauchen, die Leerzeichen enthalten dürfen. Dann muss std::noskipws her, damit er bei \0 trennt, richtig? Und später für den Stream noch brav ios::skipws zurücksetzen, falls es vorher drin war.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2260159</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2260159</guid><dc:creator><![CDATA[Eisflamme]]></dc:creator><pubDate>Sat, 13 Oct 2012 21:25:14 GMT</pubDate></item><item><title><![CDATA[Reply to Stream: mehrere Zahlen on Sun, 14 Oct 2012 15:51:55 GMT]]></title><description><![CDATA[<p>Hallo nochmal,</p>
<p>ich frage gleich nochmal hier nach. Jetzt würde ich gerne Strings in den Stream schicken, die aber mit Leerzeichen getrennt sein können sollen. Findet ihr meine folgende Lösung in Ordnung?</p>
<p>ofstream: ofs &lt;&lt; string &lt;&lt; '\0';<br />
ifstream: getline(ifs, string, '\0');</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2260327</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2260327</guid><dc:creator><![CDATA[Eisflamme]]></dc:creator><pubDate>Sun, 14 Oct 2012 15:51:55 GMT</pubDate></item></channel></rss>