<?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[Problem bei Ifstream]]></title><description><![CDATA[<p>Ich versuche folgendes zu machen :</p>
<p>1. Mit ifstream eine Datei öffnen.<br />
2. Durchzählen wieviele Zeichen drinn sind.<br />
3. Speicher reserviren für die anzahl der gezählten Zeichen.<br />
4. Datei in diesen Reservierten Speicher lesen.</p>
<p>Ich habe jetzt aber das Problem, das beim 2. mal lesen der Datei nur Müll zurückgelesen wird. Beim ersten mal wird die Datei super eingelesen, aber das 2. mal geht gar nix.</p>
<p>Ich hänge mal meinen Code an, vielleicht weiß ja jemand was es sein könnte. Ich bin noch sehr neu in c++ Filehandling.</p>
<pre><code class="language-cpp">char ch;
    int x,y,z,i;
    ifstream in(filename);
    x = 0;
    y = 0;
    z = 0;
    if(!in.is_open())
    {
       cerr &lt;&lt; &quot;map::File &quot; &lt;&lt; filename &lt;&lt;&quot; could not be opened !!&quot;;         
       exit(1);       
    }
	// First lets count, how many memory we need for the file 
	while (!in.eof())
	{
      ch=in.get();
      if (ch == 10)
      {
          y++;         //count lines
      }
      else
      {
          x++;         //count pieces
      }

      if(ch == 32)  // count spaces
      {
          z++;      
      }
    }
    x = x - 1;  // subtract last char, because of 1 loop too much

    this-&gt;lines = y;                  // store the needed values
    this-&gt;mapsize = x;                // store the needed values

    char *mapdata = new char[x];      // Reservate enough memory for the mapdata
    ifstream inn(filename);
    cout &lt;&lt; inn.tellg();
    in.seekg (0, ios::beg);
    cout &lt;&lt; inn.tellg();
    if(!in.is_open())
    {
       cerr &lt;&lt; &quot;map::File &quot; &lt;&lt; filename &lt;&lt;&quot; could not be opened !!&quot;;         
       exit(1);       
    }

    while (!in.eof())
	{
       ch=in.get();
      *mapdata = ch;
       mapdata++;
    }

    in.close();

    for (i = 0; i &lt;= this-&gt;mapsize; i++)
    {
        if(!(i %(this-&gt;mapsize / this-&gt;lines)))
        {
            cout &lt;&lt; endl;
        }
        else
        {
            cout &lt;&lt; *mapdata;    
        }
        mapdata++;
    }
</code></pre>
<p>Hier wird beim 2. mal while (!inn.eof()) anscheinend nichts oder nur Müll gelesen. Keine Ahnung warum !</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/135199/problem-bei-ifstream</link><generator>RSS for Node</generator><lastBuildDate>Fri, 28 Aug 2026 22:11:32 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/135199.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 31 Jan 2006 14:51:07 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Problem bei Ifstream on Tue, 31 Jan 2006 15:05:12 GMT]]></title><description><![CDATA[<p>Ich versuche folgendes zu machen :</p>
<p>1. Mit ifstream eine Datei öffnen.<br />
2. Durchzählen wieviele Zeichen drinn sind.<br />
3. Speicher reserviren für die anzahl der gezählten Zeichen.<br />
4. Datei in diesen Reservierten Speicher lesen.</p>
<p>Ich habe jetzt aber das Problem, das beim 2. mal lesen der Datei nur Müll zurückgelesen wird. Beim ersten mal wird die Datei super eingelesen, aber das 2. mal geht gar nix.</p>
<p>Ich hänge mal meinen Code an, vielleicht weiß ja jemand was es sein könnte. Ich bin noch sehr neu in c++ Filehandling.</p>
<pre><code class="language-cpp">char ch;
    int x,y,z,i;
    ifstream in(filename);
    x = 0;
    y = 0;
    z = 0;
    if(!in.is_open())
    {
       cerr &lt;&lt; &quot;map::File &quot; &lt;&lt; filename &lt;&lt;&quot; could not be opened !!&quot;;         
       exit(1);       
    }
	// First lets count, how many memory we need for the file 
	while (!in.eof())
	{
      ch=in.get();
      if (ch == 10)
      {
          y++;         //count lines
      }
      else
      {
          x++;         //count pieces
      }

      if(ch == 32)  // count spaces
      {
          z++;      
      }
    }
    x = x - 1;  // subtract last char, because of 1 loop too much

    this-&gt;lines = y;                  // store the needed values
    this-&gt;mapsize = x;                // store the needed values

    char *mapdata = new char[x];      // Reservate enough memory for the mapdata
    ifstream inn(filename);
    cout &lt;&lt; inn.tellg();
    in.seekg (0, ios::beg);
    cout &lt;&lt; inn.tellg();
    if(!in.is_open())
    {
       cerr &lt;&lt; &quot;map::File &quot; &lt;&lt; filename &lt;&lt;&quot; could not be opened !!&quot;;         
       exit(1);       
    }

    while (!in.eof())
	{
       ch=in.get();
      *mapdata = ch;
       mapdata++;
    }

    in.close();

    for (i = 0; i &lt;= this-&gt;mapsize; i++)
    {
        if(!(i %(this-&gt;mapsize / this-&gt;lines)))
        {
            cout &lt;&lt; endl;
        }
        else
        {
            cout &lt;&lt; *mapdata;    
        }
        mapdata++;
    }
</code></pre>
<p>Hier wird beim 2. mal while (!inn.eof()) anscheinend nichts oder nur Müll gelesen. Keine Ahnung warum !</p>
]]></description><link>https://www.c-plusplus.net/forum/post/982113</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/982113</guid><dc:creator><![CDATA[Tom555]]></dc:creator><pubDate>Tue, 31 Jan 2006 15:05:12 GMT</pubDate></item><item><title><![CDATA[Reply to Problem bei Ifstream on Tue, 31 Jan 2006 14:59:59 GMT]]></title><description><![CDATA[<p>1. Fehler:</p>
<p>Wenn der Stream nicht im good-Zustand ist, macht das seekg genau garnichts. Weil er zumindest zu dem Zeitpunkt wo du mit dem Lesen fertig bist das eof-Bit gesetzt hat, macht das seekg garnichts. Also: mit .clear() den Fehlerstatus zurücksetzen.</p>
<p>2. Fehler:</p>
<pre><code class="language-cpp">while (!inn.eof())
    {
       ch=in.get();
      *mapdata = ch;
       mapdata++;
    }
</code></pre>
<p>Nachdem du mit get gelesen hast, musst du auf eof prüfen. Du verarbeitest das Zeichen aber einfach. Zudem liefert in.get() einen int zurück und keinen char. D.h. im eof-Fall hast du hier z.B. einen Informationsverlust. Du musst hier mit den Traits arbeiten, um das Zeichen korrekt auf Fehler zu prüfen und dann in einen char zu wandeln.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/982120</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/982120</guid><dc:creator><![CDATA[7H3 N4C3R]]></dc:creator><pubDate>Tue, 31 Jan 2006 14:59:59 GMT</pubDate></item><item><title><![CDATA[Reply to Problem bei Ifstream on Tue, 31 Jan 2006 15:17:56 GMT]]></title><description><![CDATA[<p>Kannst du mir vielleicht nen link geben, wo es ein tutorial oder so gibt für Filehandling mit c++ ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/982138</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/982138</guid><dc:creator><![CDATA[Tom555]]></dc:creator><pubDate>Tue, 31 Jan 2006 15:17:56 GMT</pubDate></item><item><title><![CDATA[Reply to Problem bei Ifstream on Tue, 31 Jan 2006 15:30:18 GMT]]></title><description><![CDATA[<p>Schau erstmal in die C++-FAQ:<br />
<a href="http://www.c-plusplus.net/forum/viewtopic-var-t-is-39469.html" rel="nofollow">http://www.c-plusplus.net/forum/viewtopic-var-t-is-39469.html</a></p>
<p>Falls dir das noch nicht ausreicht, schau mal hier:<br />
<a href="http://fara.cs.uni-potsdam.de/~kaufmann/?page=GenCppFaqs&amp;faq=eof#Answ" rel="nofollow">http://fara.cs.uni-potsdam.de/~kaufmann/?page=GenCppFaqs&amp;faq=eof#Answ</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/982152</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/982152</guid><dc:creator><![CDATA[7H3 N4C3R]]></dc:creator><pubDate>Tue, 31 Jan 2006 15:30:18 GMT</pubDate></item><item><title><![CDATA[Reply to Problem bei Ifstream on Tue, 31 Jan 2006 16:37:45 GMT]]></title><description><![CDATA[<p>Danke !</p>
]]></description><link>https://www.c-plusplus.net/forum/post/982254</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/982254</guid><dc:creator><![CDATA[Tom555]]></dc:creator><pubDate>Tue, 31 Jan 2006 16:37:45 GMT</pubDate></item><item><title><![CDATA[Reply to Problem bei Ifstream on Tue, 31 Jan 2006 20:41:47 GMT]]></title><description><![CDATA[<p>Tom555 schrieb:</p>
<blockquote>
<p>Ich versuche folgendes zu machen :</p>
<p>1. Mit ifstream eine Datei öffnen.<br />
2. Durchzählen wieviele Zeichen drinn sind.<br />
3. Speicher reserviren für die anzahl der gezählten Zeichen.<br />
4. Datei in diesen Reservierten Speicher lesen.</p>
</blockquote>
<p>Also so</p>
<pre><code class="language-cpp">#include &lt;algorithm&gt;    // copy, distance
#include &lt;fstream&gt;
#include &lt;iterator&gt;     // istreambuf_iterator

int main()
{
    using namespace std;
    const int anz = distance( istreambuf_iterator&lt; char &gt;( ifstream( &quot;input.txt&quot; ).rdbuf() ),
        istreambuf_iterator&lt; char &gt;() );
    char* mapdata = new char[anz];      // besser boost::shared_array
    copy( istreambuf_iterator&lt; char &gt;( ifstream( &quot;input.txt&quot; ).rdbuf() ),
        istreambuf_iterator&lt; char &gt;(), mapdata );
    delete[] mapdata;
    return 0;
}
</code></pre>
<p>noch einfacher wäre es, wenn Du es so machst:</p>
<pre><code class="language-cpp">#include &lt;algorithm&gt;    // copy
#include &lt;fstream&gt;
#include &lt;iterator&gt;     // istreambuf_iterator, back_inserter
#include &lt;vector&gt;

int main()
{
    using namespace std;
    vector&lt; char &gt; mapdata;
    copy( istreambuf_iterator&lt; char &gt;( ifstream( &quot;input.txt&quot; ).rdbuf() ),
        istreambuf_iterator&lt; char &gt;(), back_inserter( mapdata ) );

    char* mapdata2 = &amp;mapdata[0];   // zeigt auf den Anfang des reservierten Speichers
    const int anz = mapdata.size(); // Anzahl der Zeichen
    return 0;
}
</code></pre>
<p>Gruß<br />
Werner</p>
]]></description><link>https://www.c-plusplus.net/forum/post/982536</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/982536</guid><dc:creator><![CDATA[Werner Salomon]]></dc:creator><pubDate>Tue, 31 Jan 2006 20:41:47 GMT</pubDate></item><item><title><![CDATA[Reply to Problem bei Ifstream on Tue, 31 Jan 2006 22:35:12 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/7393">@werner</a><br />
Cool, danke. Das werd ich morgen gleich mal ausprobieren.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/982615</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/982615</guid><dc:creator><![CDATA[Tom555]]></dc:creator><pubDate>Tue, 31 Jan 2006 22:35:12 GMT</pubDate></item></channel></rss>