<?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[Inhalt aus Datei lesen]]></title><description><![CDATA[<p>Welche Klasse sollte man benutzen um lesend auf eine Datei zuzugreifen?<br />
Wie kann ich den gesamten Inhalt auf einmal auslesen?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/307959/inhalt-aus-datei-lesen</link><generator>RSS for Node</generator><lastBuildDate>Thu, 06 Aug 2026 08:08:25 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/307959.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 10 Sep 2012 16:23:43 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Inhalt aus Datei lesen on Mon, 10 Sep 2012 16:23:43 GMT]]></title><description><![CDATA[<p>Welche Klasse sollte man benutzen um lesend auf eine Datei zuzugreifen?<br />
Wie kann ich den gesamten Inhalt auf einmal auslesen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2250014</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2250014</guid><dc:creator><![CDATA[asfsagsdgsg]]></dc:creator><pubDate>Mon, 10 Sep 2012 16:23:43 GMT</pubDate></item><item><title><![CDATA[Reply to Inhalt aus Datei lesen on Mon, 10 Sep 2012 16:28:11 GMT]]></title><description><![CDATA[<p>asfsagsdgsg schrieb:</p>
<blockquote>
<p>Welche Klasse sollte man benutzen um lesend auf eine Datei zuzugreifen?</p>
</blockquote>
<p>std::ifstream</p>
<blockquote>
<p>Wie kann ich den gesamten Inhalt auf einmal auslesen?</p>
</blockquote>
<p>Kommt drauf an, wo der Inhalt landen soll. In Frage kommt vieles, von rd_buf über read zu istream_iteratoren oder einfach eine gute alte Leseschleife bis das Lesen fehlschlägt (d.h. in der Regel am Dateiende).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2250017</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2250017</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 10 Sep 2012 16:28:11 GMT</pubDate></item><item><title><![CDATA[Reply to Inhalt aus Datei lesen on Mon, 10 Sep 2012 16:30:09 GMT]]></title><description><![CDATA[<p>Der Inhalt soll einfach in einem std::string gespeichert werden, denke da reicht einfach read bis EOF?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2250018</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2250018</guid><dc:creator><![CDATA[asfsagsdgsg]]></dc:creator><pubDate>Mon, 10 Sep 2012 16:30:09 GMT</pubDate></item><item><title><![CDATA[Reply to Inhalt aus Datei lesen on Mon, 10 Sep 2012 16:51:56 GMT]]></title><description><![CDATA[<p>asfsagsdgsg schrieb:</p>
<blockquote>
<p>Der Inhalt soll einfach in einem std::string gespeichert werden, denke da reicht einfach read bis EOF?</p>
</blockquote>
<p>Am effektivsten sind diese beiden Varianten:</p>
<pre><code class="language-cpp">// Variante 1:
     ifstream file(&quot;test.txt&quot;);
     string str(static_cast&lt;stringstream const&amp;&gt;(stringstream() &lt;&lt; file.rdbuf()).str());
</code></pre>
<p>und</p>
<pre><code class="language-cpp">// Variante 4: istreambuf_iterator + ostreambuf_iterator
    ifstream file(&quot;test.txt&quot;);
    string str( (istreambuf_iterator&lt;char&gt;(file)), (istreambuf_iterator&lt;char&gt;()) );
</code></pre>
<p>Beide Varianten dürften gleich schnell sein.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2250026</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2250026</guid><dc:creator><![CDATA[out]]></dc:creator><pubDate>Mon, 10 Sep 2012 16:51:56 GMT</pubDate></item><item><title><![CDATA[Reply to Inhalt aus Datei lesen on Mon, 10 Sep 2012 16:53:02 GMT]]></title><description><![CDATA[<p>sry, da sollte natürlich <code>Variante 2:</code> stehen (war noch aus nem alten thread).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2250027</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2250027</guid><dc:creator><![CDATA[out]]></dc:creator><pubDate>Mon, 10 Sep 2012 16:53:02 GMT</pubDate></item><item><title><![CDATA[Reply to Inhalt aus Datei lesen on Mon, 10 Sep 2012 16:59:29 GMT]]></title><description><![CDATA[<p>Da fände ich ein einfaches read aber wesentlich lesbarer und ist genau so schnell. Es könnte sogar minimal schneller sein, weil man sich Reallokationen spart.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2250028</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2250028</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 10 Sep 2012 16:59:29 GMT</pubDate></item><item><title><![CDATA[Reply to Inhalt aus Datei lesen on Mon, 10 Sep 2012 17:25:03 GMT]]></title><description><![CDATA[<p>SeppJ schrieb:</p>
<blockquote>
<p>Da fände ich ein einfaches read aber wesentlich lesbarer und ist genau so schnell. Es könnte sogar minimal schneller sein, weil man sich Reallokationen spart.</p>
</blockquote>
<p>Würdest du diese Variante kurz machen und postenbitte, bei mir mag es gerade nicht funktionieren. <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/2250040</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2250040</guid><dc:creator><![CDATA[out]]></dc:creator><pubDate>Mon, 10 Sep 2012 17:25:03 GMT</pubDate></item><item><title><![CDATA[Reply to Inhalt aus Datei lesen on Mon, 10 Sep 2012 17:33:26 GMT]]></title><description><![CDATA[<p>out schrieb:</p>
<blockquote>
<p>SeppJ schrieb:</p>
<blockquote>
<p>Da fände ich ein einfaches read aber wesentlich lesbarer und ist genau so schnell. Es könnte sogar minimal schneller sein, weil man sich Reallokationen spart.</p>
</blockquote>
<p>Würdest du diese Variante kurz machen und postenbitte, bei mir mag es gerade nicht funktionieren. <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>
</blockquote>
<p>Ok, habs doch noch hinbekommen. Also hier noch, die von SeppJ angesprochene, Variante 3:</p>
<pre><code class="language-cpp">// Variante 3: read
        ifstream file(&quot;test.txt&quot;);
		string str;
        char buffer[8192];
		while( file.read(buffer, sizeof(buffer)), file.gcount()&gt;0 )
		{
			str.append(buffer, sizeof(buffer));
		}
</code></pre>
<p>Sie ist in der Tat etwas schneller.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2250044</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2250044</guid><dc:creator><![CDATA[out]]></dc:creator><pubDate>Mon, 10 Sep 2012 17:33:26 GMT</pubDate></item><item><title><![CDATA[Reply to Inhalt aus Datei lesen on Mon, 10 Sep 2012 17:41:10 GMT]]></title><description><![CDATA[<p>Ich hätte ja jetzt gleich den string auf die Dateigröße resized(bzw. sofort mit dieser Größe erstell) und dann gnadenlos direkt in diesen String geschrieben. Wenn die 8k-Blöcke schon noch etwas gebracht haben, dann wird dies nochmal schneller sein.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2250045</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2250045</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 10 Sep 2012 17:41:10 GMT</pubDate></item><item><title><![CDATA[Reply to Inhalt aus Datei lesen on Mon, 10 Sep 2012 18:03:20 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">std::string FileReader::get_file_content(std::string &amp;file) const {

	std::ifstream f(file);
	std::string   content;

	f.seekg(0, std::ios::end);
	auto length = f.tellg();
	f.seekg(0, std::ios::beg);

	content.resize(length);
	f.read(const_cast&lt;char *&gt;(content.c_str()), length);

	return content;
}
</code></pre>
<p>Ist das so in Ordnung?<br />
Bezüglich des const_cast: Eigentlich sollte man sowas ja bleiben lassen, aber in diesem Fall dürfte das doch kein Problem darstellen, oder?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2250050</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2250050</guid><dc:creator><![CDATA[asfsagsdgsg]]></dc:creator><pubDate>Mon, 10 Sep 2012 18:03:20 GMT</pubDate></item><item><title><![CDATA[Reply to Inhalt aus Datei lesen on Mon, 10 Sep 2012 18:14:55 GMT]]></title><description><![CDATA[<p>Wieso nicht einfach so?</p>
<pre><code class="language-cpp">ifstream file(&quot;test.txt&quot;);
		file.seekg (0, ios::end);
		const size_t size = file.tellg();
		file.seekg (0, ios::beg);

		string str;
		str.resize( size );
		file.read( &amp;str[0], size);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2250053</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2250053</guid><dc:creator><![CDATA[out]]></dc:creator><pubDate>Mon, 10 Sep 2012 18:14:55 GMT</pubDate></item><item><title><![CDATA[Reply to Inhalt aus Datei lesen on Tue, 11 Sep 2012 06:53:24 GMT]]></title><description><![CDATA[<p>out schrieb:</p>
<blockquote>
<pre><code class="language-cpp">// Variante 3: read
        ifstream file(&quot;test.txt&quot;);
		string str;
        char buffer[8192];
		while( file.read(buffer, sizeof(buffer)), file.gcount()&gt;0 )
		{
			str.append(buffer, sizeof(buffer));
		}
</code></pre>
</blockquote>
<p>Wird hier nicht Dein String zu lang, wenn die Dateigröße nicht durch 8K teilbar ist?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2250147</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2250147</guid><dc:creator><![CDATA[Belli]]></dc:creator><pubDate>Tue, 11 Sep 2012 06:53:24 GMT</pubDate></item></channel></rss>