<?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[Fehlerhafte Daten beim einlesen von einer exe Datei]]></title><description><![CDATA[<p>Guten Abend!</p>
<p>Ich hab da ein kleines Problem und komm einfach nicht drauf warum es nicht funktioniert.</p>
<p>Hier mal mein Code:</p>
<pre><code>char*	openfile(char* path)
{
	char				buffer[512]		=	&quot;&quot;;
	char*				rbuf			=	&quot;&quot;;
	std::fstream		data(path, std::ios::in | std::ios::binary);
	int					file_size		=	0;
	int					real_size		=	0;
	std::string			tmp				=	&quot;&quot;;

	data.seekg(0L, std::ios::end);
	file_size		=	data.tellg();
	data.seekg(0L, std::ios::beg);

	tmp			=	std::to_string(file_size);
	real_size	=	((file_size + strlen(tmp.c_str())) + 1);
	tmp			=	std::to_string(real_size);
	tmp			+= &quot;\n&quot;;

	while (!data.eof())
	{
		data.read(rbuf, 512);
		tmp += buffer;
	}

	rbuf = new char[real_size];
	strncpy_s(rbuf, real_size, tmp.c_str(), real_size);
	tmp = &quot;&quot;;
	data.close();
	return rbuf;
}
</code></pre>
<p>mein Problem ist, dass die *.exe Datei zwar eingelesen wird, aber nicht richtig(also der Array wird immer mit falschen Zeichen befüllt). Liegt es vielleicht daran, dass ich einen CHAR als Dateityp verwende? <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="🙂"
    /></p>
<p>MfG,<br />
styxer</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/329189/fehlerhafte-daten-beim-einlesen-von-einer-exe-datei</link><generator>RSS for Node</generator><lastBuildDate>Tue, 07 Jul 2026 02:31:05 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/329189.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 11 Nov 2014 18:58:13 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Fehlerhafte Daten beim einlesen von einer exe Datei on Tue, 11 Nov 2014 18:58:13 GMT]]></title><description><![CDATA[<p>Guten Abend!</p>
<p>Ich hab da ein kleines Problem und komm einfach nicht drauf warum es nicht funktioniert.</p>
<p>Hier mal mein Code:</p>
<pre><code>char*	openfile(char* path)
{
	char				buffer[512]		=	&quot;&quot;;
	char*				rbuf			=	&quot;&quot;;
	std::fstream		data(path, std::ios::in | std::ios::binary);
	int					file_size		=	0;
	int					real_size		=	0;
	std::string			tmp				=	&quot;&quot;;

	data.seekg(0L, std::ios::end);
	file_size		=	data.tellg();
	data.seekg(0L, std::ios::beg);

	tmp			=	std::to_string(file_size);
	real_size	=	((file_size + strlen(tmp.c_str())) + 1);
	tmp			=	std::to_string(real_size);
	tmp			+= &quot;\n&quot;;

	while (!data.eof())
	{
		data.read(rbuf, 512);
		tmp += buffer;
	}

	rbuf = new char[real_size];
	strncpy_s(rbuf, real_size, tmp.c_str(), real_size);
	tmp = &quot;&quot;;
	data.close();
	return rbuf;
}
</code></pre>
<p>mein Problem ist, dass die *.exe Datei zwar eingelesen wird, aber nicht richtig(also der Array wird immer mit falschen Zeichen befüllt). Liegt es vielleicht daran, dass ich einen CHAR als Dateityp verwende? <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="🙂"
    /></p>
<p>MfG,<br />
styxer</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2427090</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2427090</guid><dc:creator><![CDATA[styxer]]></dc:creator><pubDate>Tue, 11 Nov 2014 18:58:13 GMT</pubDate></item><item><title><![CDATA[Reply to Fehlerhafte Daten beim einlesen von einer exe Datei on Tue, 11 Nov 2014 19:07:19 GMT]]></title><description><![CDATA[<p>So sollte deine Funktion aussehen:</p>
<pre><code class="language-cpp">std::vector&lt;char&gt; readfile(const char* path) {
  std::ifstream file(path, std::ios::binary);
  std::vector&lt;char&gt; data((std::istreambuf_iterator&lt;char&gt;(file)),
                          std::istreambuf_iterator&lt;char&gt;());
  return data;
}
</code></pre>
<p>Das entscheidende Problem an deinem Code ist &quot;tmp += buffer&quot;: buffer muss nicht nullterminiert sein. String ist ausserdem nicht die beste Datenstruktur, binäre Daten zu speichern.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2427092</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2427092</guid><dc:creator><![CDATA[vertical_alignment]]></dc:creator><pubDate>Tue, 11 Nov 2014 19:07:19 GMT</pubDate></item><item><title><![CDATA[Reply to Fehlerhafte Daten beim einlesen von einer exe Datei on Tue, 11 Nov 2014 19:12:43 GMT]]></title><description><![CDATA[<p>Super danke für die schnelle antwort. Kann ich an diesen vector am anfang noch meine FileSize anhängen, da ich die brauche. meine Funktion gehört nämlich zu einem netzwerk programm mit dem ich daten senden will und da muss ich die dateigröße mitsenden <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="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2427094</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2427094</guid><dc:creator><![CDATA[styxer]]></dc:creator><pubDate>Tue, 11 Nov 2014 19:12:43 GMT</pubDate></item><item><title><![CDATA[Reply to Fehlerhafte Daten beim einlesen von einer exe Datei on Tue, 11 Nov 2014 19:19:41 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">std::vector&lt;char&gt; file_to_something(const char* path) {
  std::vector&lt;char&gt; data;
  data.push_back(irgendeinwert);
  std::ifstream file(path, std::ios::binary);
  data.insert(data.end(),
              std::istreambuf_iterator&lt;char&gt;(file),
              std::istreambuf_iterator&lt;char&gt;());
  return data;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2427095</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2427095</guid><dc:creator><![CDATA[vertical_alignment]]></dc:creator><pubDate>Tue, 11 Nov 2014 19:19:41 GMT</pubDate></item><item><title><![CDATA[Reply to Fehlerhafte Daten beim einlesen von einer exe Datei on Tue, 11 Nov 2014 20:40:36 GMT]]></title><description><![CDATA[<pre><code>std::vector&lt;char&gt; readfile(const char* path) { 
  std::ifstream file(path, std::ios::binary); 
  std::vector&lt;char&gt; data((std::istreambuf_iterator&lt;char&gt;(file)), 
                          std::istreambuf_iterator&lt;char&gt;()); 
  return data; 
}
</code></pre>
<p>Generell eine inperformante Methode, auch wenn es hier möglicherweise keinen Unterschied macht.<br />
<a href="http://stackoverflow.com/a/2602258/3647361" rel="nofollow">Hier</a> wird gezeigt wie es besser geht.</p>
<pre><code>std::ifstream t(path, std::ios::binary); 
t.seekg(0, std::ios::end);
std::vector&lt;char&gt; buffer(t.tellg());
t.seekg(0);
t.read(&amp;buffer[0], buffer.size());
</code></pre>
<p>Das ganze kann noch in ein Template ausgelagert werden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2427108</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2427108</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Tue, 11 Nov 2014 20:40:36 GMT</pubDate></item><item><title><![CDATA[Reply to Fehlerhafte Daten beim einlesen von einer exe Datei on Tue, 11 Nov 2014 20:48:19 GMT]]></title><description><![CDATA[<p>Leider funktionieren beide Methoden nicht ganz so wie ich es mir vorstelle <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="😞"
    /></p>
<p>ich poste hier mal meine 2 funktionen (open&amp;save File) vielleicht ist es dann einfacher.</p>
<pre><code>char*	openfile(char* path)
{
	char				buffer[512]		=	&quot;&quot;;
	char*				rbuf			=	&quot;&quot;;
	std::fstream		data(path, std::ios::in | std::ios::binary);
	int					file_size		=	0;
	int					real_size		=	0;
	int					x = 0, y = 0;
	std::string			tmp				=	&quot;&quot;;

	data.seekg(0L, std::ios::end);
	file_size		=	data.tellg();
	data.seekg(0L, std::ios::beg);

	tmp			=	std::to_string(file_size);
	real_size	=	((file_size + strlen(tmp.c_str())) + 1);
	tmp.resize(real_size);
	tmp			=	std::to_string(real_size);
	tmp			+= &quot;\n&quot;;

	rbuf = new char[real_size];

	while (!data.eof())
	{
		data.read(rbuf, real_size);
	}

	strncpy_s(rbuf, real_size, tmp.c_str(), real_size);
	tmp = &quot;&quot;;
	data.close();
	return rbuf;

}
int		savefile(std::string data, char* path)
{
	int			size	=	0;

	int			x		=	0;
	int			y		=	0;

	char*		newdata =	&quot;&quot;;
	std::string tmp		=	&quot;&quot;;

	while (data[x] != '\n')
	{
		tmp += data[x];
		x++;
	}
	x++;
	while (data[x] != '\0')
	{
		data[y] = data[x];
		y++;
		x++;
	}
	data[y] = '\0';
	size = atoi(tmp.c_str());

	size = (size - strlen(tmp.c_str())) - 1;

	std::fstream FILE(path, std::ios::out | std::ios::binary);

	FILE.write(data.c_str(), size);

	FILE.close();
	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2427112</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2427112</guid><dc:creator><![CDATA[styxer]]></dc:creator><pubDate>Tue, 11 Nov 2014 20:48:19 GMT</pubDate></item><item><title><![CDATA[Reply to Fehlerhafte Daten beim einlesen von einer exe Datei on Tue, 11 Nov 2014 20:49:26 GMT]]></title><description><![CDATA[<p>Arcoth schrieb:</p>
<blockquote>
<p>Generell eine inperformante Methode, auch wenn es hier möglicherweise keinen Unterschied macht.</p>
</blockquote>
<p>Schätzungsweise 1% Performance-Unterschied ~wenn die Datei nicht im Cache liegt~?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2427113</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2427113</guid><dc:creator><![CDATA[ram vs hd]]></dc:creator><pubDate>Tue, 11 Nov 2014 20:49:26 GMT</pubDate></item></channel></rss>