<?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[long als binär in Datei speichern]]></title><description><![CDATA[<p>Hi,</p>
<p>ich würde gerne eine long Variable in einer Datei speichern, die long sollte nicht für den Menschen lesbar sein, also binär gespeichert werden, wie mach ich das?</p>
<p>Hab folgendes versucht leider ohne Erfolg, die Zahl bleibt lesbar:</p>
<pre><code>fstream savefile;
// open a new or empty file
savefile.open(&quot;config.txt&quot;, ios::out|ios::binary|ios::app);
// Wieso muss ich die Datei leeren?
savefile.clear();
if (savefile.is_open() == false)
{
	MessageBox(NULL, &quot;Daten konnten nicht gespeichert werden.&quot;, &quot;Hazard Error&quot;, MB_OK);
	return;
}

char tmp[sizeof(long)+1];
long x;
x = 150;
memcpy(tmp, &amp;x, sizeof(x));
tmp[sizeof(long)] = 0; // Muss ich die Null ans Ende vom Array schreiben?
savefile.write(tmp, sizeof(long));

savefile.close();
</code></pre>
<p>Gruß<br />
Scarabol</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/240764/long-als-binär-in-datei-speichern</link><generator>RSS for Node</generator><lastBuildDate>Mon, 06 Apr 2026 07:10:05 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/240764.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 11 May 2009 09:30:44 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to long als binär in Datei speichern on Mon, 11 May 2009 09:30:44 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>ich würde gerne eine long Variable in einer Datei speichern, die long sollte nicht für den Menschen lesbar sein, also binär gespeichert werden, wie mach ich das?</p>
<p>Hab folgendes versucht leider ohne Erfolg, die Zahl bleibt lesbar:</p>
<pre><code>fstream savefile;
// open a new or empty file
savefile.open(&quot;config.txt&quot;, ios::out|ios::binary|ios::app);
// Wieso muss ich die Datei leeren?
savefile.clear();
if (savefile.is_open() == false)
{
	MessageBox(NULL, &quot;Daten konnten nicht gespeichert werden.&quot;, &quot;Hazard Error&quot;, MB_OK);
	return;
}

char tmp[sizeof(long)+1];
long x;
x = 150;
memcpy(tmp, &amp;x, sizeof(x));
tmp[sizeof(long)] = 0; // Muss ich die Null ans Ende vom Array schreiben?
savefile.write(tmp, sizeof(long));

savefile.close();
</code></pre>
<p>Gruß<br />
Scarabol</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1708862</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1708862</guid><dc:creator><![CDATA[Scarabol]]></dc:creator><pubDate>Mon, 11 May 2009 09:30:44 GMT</pubDate></item><item><title><![CDATA[Reply to long als binär in Datei speichern on Mon, 11 May 2009 09:38:57 GMT]]></title><description><![CDATA[<p>Viel einfacher:</p>
<pre><code class="language-cpp">using std::ofstream;
//binaer und immer ueberschreiben
ofstream saveFile(&quot;config.dat&quot;, ofstream::binary|ofstream::trunc);
long x = 47110815;
if(saveFile) //wenn stream okay...
{
    //...dann schreibe.
    saveFile.write(reinterpret_cast&lt;char const*&gt;(&amp;x), sizeof(x));
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1708866</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1708866</guid><dc:creator><![CDATA[Tachyon]]></dc:creator><pubDate>Mon, 11 May 2009 09:38:57 GMT</pubDate></item><item><title><![CDATA[Reply to long als binär in Datei speichern on Mon, 11 May 2009 09:45:22 GMT]]></title><description><![CDATA[<p>Cool danke, funktioniert</p>
<p>Gruß<br />
Scarabol</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1708868</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1708868</guid><dc:creator><![CDATA[Scarabol]]></dc:creator><pubDate>Mon, 11 May 2009 09:45:22 GMT</pubDate></item><item><title><![CDATA[Reply to long als binär in Datei speichern on Mon, 11 May 2009 09:57:14 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>also ich hab 1 long und dann folgt ein String, ich weiß beim auslesen leider nicht, wie lang er ist, was geb ich dann bei read als 2. Parameter an?</p>
<p>Gruß<br />
Scarabol</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1708872</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1708872</guid><dc:creator><![CDATA[Scarabol]]></dc:creator><pubDate>Mon, 11 May 2009 09:57:14 GMT</pubDate></item><item><title><![CDATA[Reply to long als binär in Datei speichern on Mon, 11 May 2009 09:59:21 GMT]]></title><description><![CDATA[<p>Du musst beim speichern entweder die Länge angeben, oder eine End of String Zeichen machen, z.B. \0.</p>
<p>Simon</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1708873</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1708873</guid><dc:creator><![CDATA[theta]]></dc:creator><pubDate>Mon, 11 May 2009 09:59:21 GMT</pubDate></item><item><title><![CDATA[Reply to long als binär in Datei speichern on Mon, 11 May 2009 10:05:03 GMT]]></title><description><![CDATA[<p>Schon geschehen, aber welche Funktion liest bis zu einem EndOfString Zeichen?</p>
<p>Gruß<br />
Scarabol</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1708875</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1708875</guid><dc:creator><![CDATA[Scarabol]]></dc:creator><pubDate>Mon, 11 May 2009 10:05:03 GMT</pubDate></item><item><title><![CDATA[Reply to long als binär in Datei speichern on Mon, 11 May 2009 11:19:21 GMT]]></title><description><![CDATA[<p>char für char und jedesmal checken.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1708904</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1708904</guid><dc:creator><![CDATA[theta]]></dc:creator><pubDate>Mon, 11 May 2009 11:19:21 GMT</pubDate></item><item><title><![CDATA[Reply to long als binär in Datei speichern on Mon, 11 May 2009 12:06:59 GMT]]></title><description><![CDATA[<p>theta schrieb:</p>
<blockquote>
<p>char für char und jedesmal checken.</p>
</blockquote>
<p>Das bringt gar nichts, bei binär geschriebenen Daten.<br />
Du musst entweder wissen, wie groß die geschriebenen Daten an einer gegebenen Position sind, oder Du musst eindeutige Trennsequenzen einbauen. Ein einzelnes Zeichen ist dafür ungeeignet.<br />
Beispiel:</p>
<pre><code class="language-cpp">long x = 11141290; //0xAA00AA, wo wird gestoppt?
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1708938</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1708938</guid><dc:creator><![CDATA[Tachyon]]></dc:creator><pubDate>Mon, 11 May 2009 12:06:59 GMT</pubDate></item><item><title><![CDATA[Reply to long als binär in Datei speichern on Mon, 11 May 2009 12:27:14 GMT]]></title><description><![CDATA[<p>Tachyon schrieb:</p>
<blockquote>
<p>theta schrieb:</p>
<blockquote>
<p>char für char und jedesmal checken.</p>
</blockquote>
<p>Das bringt gar nichts, bei binär geschriebenen Daten.<br />
Du musst entweder wissen, wie groß die geschriebenen Daten an einer gegebenen Position sind, oder Du musst eindeutige Trennsequenzen einbauen. Ein einzelnes Zeichen ist dafür ungeeignet.<br />
Beispiel:</p>
<pre><code class="language-cpp">long x = 11141290; //0xAA00AA, wo wird gestoppt?
</code></pre>
</blockquote>
<p>Doch, wenn er ja weiss, dass er als nächstes einen String einliest, liest er von da an bis zum null. Er muss wissen was er einliesst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1708953</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1708953</guid><dc:creator><![CDATA[theta]]></dc:creator><pubDate>Mon, 11 May 2009 12:27:14 GMT</pubDate></item><item><title><![CDATA[Reply to long als binär in Datei speichern on Mon, 11 May 2009 16:10:19 GMT]]></title><description><![CDATA[<p>Vielen Dank an alle,</p>
<p>da mir das mit der Null dann doch zu kompliziert war hab ich einfach die Länge mit abgespeichert, ging schneller, einfach und ist obendrein noch sicherer.</p>
<p>Gruß<br />
Scarabol</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1709084</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1709084</guid><dc:creator><![CDATA[Scarabol]]></dc:creator><pubDate>Mon, 11 May 2009 16:10:19 GMT</pubDate></item><item><title><![CDATA[Reply to long als binär in Datei speichern on Mon, 11 May 2009 16:19:41 GMT]]></title><description><![CDATA[<p>theta schrieb:</p>
<blockquote>
<p>Doch, wenn er ja weiss, dass er als nächstes einen String einliest, liest er von da an bis zum null. Er muss wissen was er einliesst.</p>
</blockquote>
<p>Jo, hast recht. Das er einen String nach dem long speichern will ist mir total entgangen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1709090</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1709090</guid><dc:creator><![CDATA[Tachyon]]></dc:creator><pubDate>Mon, 11 May 2009 16:19:41 GMT</pubDate></item></channel></rss>