<?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[Textdatei öffnen und auslesen]]></title><description><![CDATA[<p>Hallo</p>
<p>auch wenn das Thema schon einige male in ähnlicher form behandelt wurde versteh ich das noch nicht so richtig.</p>
<p>Ich habe eine Textdatei input.txt in der folgende Daten enthalten sind:</p>
<p>1,2,3<br />
4,5,6<br />
7,8,9</p>
<p>Diesen Datensatz würde ich jetzt gerne als 3x3 int array einlesen.<br />
Wie gehe ich dabei am besten vor?</p>
<p>Gruß</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/276172/textdatei-öffnen-und-auslesen</link><generator>RSS for Node</generator><lastBuildDate>Wed, 26 Aug 2026 10:56:00 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/276172.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 28 Oct 2010 08:59:51 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Textdatei öffnen und auslesen on Thu, 28 Oct 2010 08:59:51 GMT]]></title><description><![CDATA[<p>Hallo</p>
<p>auch wenn das Thema schon einige male in ähnlicher form behandelt wurde versteh ich das noch nicht so richtig.</p>
<p>Ich habe eine Textdatei input.txt in der folgende Daten enthalten sind:</p>
<p>1,2,3<br />
4,5,6<br />
7,8,9</p>
<p>Diesen Datensatz würde ich jetzt gerne als 3x3 int array einlesen.<br />
Wie gehe ich dabei am besten vor?</p>
<p>Gruß</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1971837</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1971837</guid><dc:creator><![CDATA[datensalat]]></dc:creator><pubDate>Thu, 28 Oct 2010 08:59:51 GMT</pubDate></item><item><title><![CDATA[Reply to Textdatei öffnen und auslesen on Thu, 28 Oct 2010 09:13:50 GMT]]></title><description><![CDATA[<p>datensalat schrieb:</p>
<blockquote>
<p>auch wenn das Thema schon einige male in ähnlicher form behandelt wurde ...</p>
</blockquote>
<p>stimmt, war schon ein paar Mal:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;fstream&gt;

template&lt; char C &gt;
std::istream&amp; Char( std::istream&amp; in )
{
    char c;
    if( in &gt;&gt; c &amp;&amp; c != C )
        in.setstate( std::ios_base::failbit );
    return in;
}

int main()
{
    using namespace std;
    int arr[3][3];
    ifstream file( &quot;input.txt&quot; );
    if( !file.is_open() )
    {
        cerr &lt;&lt; &quot;Fehler beim Oeffnen&quot; &lt;&lt; endl;
        return -1;
    }
    for( int iZl = 0; iZl &lt; 3 
        &amp;&amp; file &gt;&gt; arr[iZl][0] &gt;&gt; Char&lt;','&gt; &gt;&gt; arr[iZl][1] &gt;&gt; Char&lt;','&gt; &gt;&gt; arr[iZl][2]; ++iZl )
        ;
    if( file )
    {
        cout &lt;&lt; &quot;alle iO&quot; &lt;&lt; endl;
        // ...
    }
};
</code></pre>
<p>was verstehst Du nicht?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1971840</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1971840</guid><dc:creator><![CDATA[Werner_logoff]]></dc:creator><pubDate>Thu, 28 Oct 2010 09:13:50 GMT</pubDate></item><item><title><![CDATA[Reply to Textdatei öffnen und auslesen on Thu, 28 Oct 2010 09:24:52 GMT]]></title><description><![CDATA[<p>Was macht dieses Konstrukt:??</p>
<pre><code class="language-cpp">std::istream&amp; Char( std::istream&amp; in )
{
    char c;
    if( in &gt;&gt; c &amp;&amp; c != C )
        in.setstate( std::ios_base::failbit );
    return in;
}
</code></pre>
<p>Was geschieht hier:</p>
<pre><code class="language-cpp">&amp;&amp; file
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1971843</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1971843</guid><dc:creator><![CDATA[datensalat]]></dc:creator><pubDate>Thu, 28 Oct 2010 09:24:52 GMT</pubDate></item><item><title><![CDATA[Reply to Textdatei öffnen und auslesen on Thu, 28 Oct 2010 11:06:27 GMT]]></title><description><![CDATA[<p>datensalat schrieb:</p>
<blockquote>
<p>Was macht dieses Konstrukt:??</p>
<pre><code class="language-cpp">std::istream&amp; Char( std::istream&amp; in )
{
    char c;
    if( in &gt;&gt; c &amp;&amp; c != C )
        in.setstate( std::ios_base::failbit );
    return in;
}
</code></pre>
</blockquote>
<p>Char&lt;&gt; ist eine sogenannte Manipulator-Funktion - genau wie z.B. <a href="http://www.cplusplus.com/reference/iostream/manipulators/hex/" rel="nofollow">hex</a>.<br />
Sie liest ein Zeichen 'c' ein. Geschieht dies ohne Fehler, so wird das Zeichen mit der Konstanten 'C' verglichen und falls die Zeichen ungleich sind, so wird das Fehlerflag im Stream 'in' gesetzt.</p>
<p>Das ganze überliest ein konkretes Zeichen und falls das Zeichen das erwartete war, geschieht sonst nichts.</p>
<p>datensalat schrieb:</p>
<blockquote>
<p>Was geschieht hier:</p>
<pre><code class="language-cpp">&amp;&amp; file
</code></pre>
</blockquote>
<p>'&amp;&amp;' ist ein logisches UND. rechts und links davon sollten nach bool konvertierbare Ausdrücke stehen. Links steht</p>
<pre><code class="language-cpp">iZl &lt; 3
</code></pre>
<p>und rechts</p>
<pre><code class="language-cpp">file &gt;&gt; arr[iZl][0] &gt;&gt; Char&lt;','&gt; &gt;&gt; arr[iZl][1] &gt;&gt; Char&lt;','&gt; &gt;&gt; arr[iZl][2]
</code></pre>
<p>Ist der linke Ausdruck WAHR, so wird der rechte ausgewertet, d.h. aus file werden die drei Zahlen gelesen und 'file' wird als Referenz zurück geliefert. 'file' kann danach in einen bool konvertiert werden und der liefert WAHR, wenn das Einlesen bisher gut gegangen ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1971879</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1971879</guid><dc:creator><![CDATA[Werner_logoff]]></dc:creator><pubDate>Thu, 28 Oct 2010 11:06:27 GMT</pubDate></item><item><title><![CDATA[Reply to Textdatei öffnen und auslesen on Thu, 28 Oct 2010 11:38:19 GMT]]></title><description><![CDATA[<p>Ich habe auch eine Frage zu dem Code:</p>
<p>Warum das:</p>
<pre><code>template&lt; char C &gt;
std::istream&amp; Char( std::istream&amp; in )
</code></pre>
<p>statt dem:</p>
<pre><code>std::istream&amp; Char( std::istream&amp; in, char C )
</code></pre>
<p>?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1971893</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1971893</guid><dc:creator><![CDATA[neonew]]></dc:creator><pubDate>Thu, 28 Oct 2010 11:38:19 GMT</pubDate></item><item><title><![CDATA[Reply to Textdatei öffnen und auslesen on Thu, 28 Oct 2010 13:09:29 GMT]]></title><description><![CDATA[<p>neonew schrieb:</p>
<blockquote>
<p>Warum das:</p>
<pre><code>template&lt; char C &gt;
std::istream&amp; Char( std::istream&amp; in )
</code></pre>
<p>statt dem:</p>
<pre><code>std::istream&amp; Char( std::istream&amp; in, char C )
</code></pre>
</blockquote>
<p>weil letzteres keine Manipulator-Funktion mehr ist. In diesem konkreten Fall müsste man schreiben</p>
<pre><code class="language-cpp">Char( Char( file &gt;&gt; arr[iZl][0], ',') &gt;&gt; arr[iZl][1], ',') &gt;&gt; arr[iZl][2]
</code></pre>
<p>Die Alternative wäre ein Manipulator mit einem Parametern; das ist einfach mehr Code.</p>
<p>Notwendig wäre so was nur dann, wenn das genaue Dateiformat (in diesem Fall das Trenner-Zeichen) zur Compile-Zeit des lesenden Programms nicht feststeht - und das ist ziemlich ungewöhnlich.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1971929</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1971929</guid><dc:creator><![CDATA[Werner_logoff]]></dc:creator><pubDate>Thu, 28 Oct 2010 13:09:29 GMT</pubDate></item><item><title><![CDATA[Reply to Textdatei öffnen und auslesen on Thu, 28 Oct 2010 13:52:19 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">std::istream&amp; Char( std::istream&amp; in )
{
    char c;
    if( in &gt;&gt; c &amp;&amp; c != C )
        in.setstate( std::ios_base::failbit );
    return in;
}
</code></pre>
<p>sieht aber falsch aus...<br />
wenn in z.B. leer ist</p>
<pre><code class="language-cpp">std::istream&amp; Char( std::istream&amp; in )
{
    char c;
    in &gt;&gt; c;
    if( !in || c != C )
        in.setstate( std::ios_base::failbit );
    return in;
}
</code></pre>
<p>oder hab ich da nen denkfehler?</p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1971951</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1971951</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Thu, 28 Oct 2010 13:52:19 GMT</pubDate></item><item><title><![CDATA[Reply to Textdatei öffnen und auslesen on Thu, 28 Oct 2010 14:44:22 GMT]]></title><description><![CDATA[<p>unskilled schrieb:</p>
<blockquote>
<p>hab ich da nen denkfehler?</p>
</blockquote>
<p>Ja <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="😃"
    /> - siehe auch <a href="http://www.c-plusplus.net/forum/viewtopic-var-p-is-1348863.html#1348863" rel="nofollow">http://www.c-plusplus.net/forum/viewtopic-var-p-is-1348863.html#1348863</a> .. immer wieder erstaunlich, wie schwierig so ein kleines harmloses logisches UND im if-Statement sein kann.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1971976</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1971976</guid><dc:creator><![CDATA[Werner_logoff]]></dc:creator><pubDate>Thu, 28 Oct 2010 14:44:22 GMT</pubDate></item><item><title><![CDATA[Reply to Textdatei öffnen und auslesen on Thu, 28 Oct 2010 16:50:39 GMT]]></title><description><![CDATA[<p>Hi<br />
also, wie es funktionieren sollte ist mir jetz glaube ich soweit klar.<br />
Das ganze soll natürlich auch eine schöne grafische oberfläche haben. Daher hab ich mir mit dem borland builder eine Form mit Button angelegt....<br />
hab dann mal folgendes versucht:</p>
<pre><code class="language-cpp">//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource &quot;*.dfm&quot;
#include &lt;iostream&gt;
#include &lt;fstream&gt;

TForm1 *Form1;

// own----------------------------------------------------------------------
template&lt; char C &gt;
std::istream&amp; Charq( std::istream&amp; in )
{
    char c;
    if( in &gt;&gt; c &amp;&amp; c != C )
        in.setstate( std::ios_base::failbit );
    return in;
}
// own----------------------------------------------------------------------
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    using namespace std;
    int arr[3][3];
    ifstream file( &quot;input.txt&quot; );
    if( !file.is_open() )
    {
        cerr &lt;&lt; &quot;Fehler beim Oeffnen&quot; &lt;&lt; endl;
    }
    for( int iZl = 0; iZl &lt; 3
        &amp;&amp; file &gt;&gt; arr[iZl][0] &gt;&gt; Charq &lt;','&gt; &gt;&gt; arr[iZl][1] &gt;&gt; Charq &lt;','&gt; &gt;&gt; arr[iZl][2]; ++iZl )
        ;
}
//---------------------------------------------------------------------------
</code></pre>
<p>Das Char musste ich umbenennen, da er mir immer einen Fehler gebracht hat.<br />
Wenn ich den gezeigten Code compilieren will kommt jetzt folgender fehler:</p>
<p>[C++ Fehler] E2299 Template-Spezialisierung kann aus '_fastcall operator &gt;&gt;(int,const Variant &amp;)' nicht generiert werden</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1972043</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1972043</guid><dc:creator><![CDATA[datensalat]]></dc:creator><pubDate>Thu, 28 Oct 2010 16:50:39 GMT</pubDate></item><item><title><![CDATA[Reply to Textdatei öffnen und auslesen on Fri, 29 Oct 2010 01:46:13 GMT]]></title><description><![CDATA[<p>Werner_logoff schrieb:</p>
<blockquote>
<p>unskilled schrieb:</p>
<blockquote>
<p>hab ich da nen denkfehler?</p>
</blockquote>
<p>Ja <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="😃"
    /> - siehe auch <a href="http://www.c-plusplus.net/forum/viewtopic-var-p-is-1348863.html#1348863" rel="nofollow">http://www.c-plusplus.net/forum/viewtopic-var-p-is-1348863.html#1348863</a> .. immer wieder erstaunlich, wie schwierig so ein kleines harmloses logisches UND im if-Statement sein kann.</p>
</blockquote>
<p>naja - es war weniger das als das automatische failbit setzen des streams</p>
<blockquote>
<p>Wenn !(in &gt;&gt; t), dann wird zwar das setstate() nicht mehr ausgeführt, aber das braucht's auch nicht, weil in dann sowieso schon den richtigen (=&quot;falschen&quot; <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="😉"
    /> ) state hat.</p>
</blockquote>
<p>hat mir dann aber doch auf die sprünge geholfen^^</p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1972175</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1972175</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Fri, 29 Oct 2010 01:46:13 GMT</pubDate></item><item><title><![CDATA[Reply to Textdatei öffnen und auslesen on Fri, 29 Oct 2010 07:47:02 GMT]]></title><description><![CDATA[<p>datensalat schrieb:</p>
<blockquote>
<p>[C++ Fehler] E2299 Template-Spezialisierung kann aus '_fastcall operator &gt;&gt;(int,const Variant &amp;)' nicht generiert werden</p>
</blockquote>
<p>Dein Compiler spinnt, der Typ <strong>Variant</strong> kommt doch gar nicht im Code vor!?</p>
<p>Ändere die for-Schleife in Zeile 34-36 nach</p>
<pre><code class="language-cpp">for( int iZl = 0; iZl &lt; 3; ++iZl )
    {
        file &gt;&gt; arr[iZl][0];
        file &gt;&gt; Char&lt;','&gt;;
        file &gt;&gt; arr[iZl][1];
        file &gt;&gt; Char&lt;','&gt;;
        file &gt;&gt; arr[iZl][2];
        if( !file )
            break;
    }
</code></pre>
<p>und sage uns dann in welcher der Zeilen der Fehler auftritt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1972224</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1972224</guid><dc:creator><![CDATA[Werner_logoff]]></dc:creator><pubDate>Fri, 29 Oct 2010 07:47:02 GMT</pubDate></item><item><title><![CDATA[Reply to Textdatei öffnen und auslesen on Fri, 29 Oct 2010 16:18:54 GMT]]></title><description><![CDATA[<p>In folgender Zeile kommt der Fehler:</p>
<pre><code class="language-cpp">file &gt;&gt; Charq&lt;','&gt;;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1972420</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1972420</guid><dc:creator><![CDATA[datensalat]]></dc:creator><pubDate>Fri, 29 Oct 2010 16:18:54 GMT</pubDate></item><item><title><![CDATA[Reply to Textdatei öffnen und auslesen on Fri, 29 Oct 2010 18:17:32 GMT]]></title><description><![CDATA[<p>Das scheint ein BCB spezifisches Problem zu sein. Ich konnte das auch nachvollziehen. Ich habe folgenden Workaraound gefunden.</p>
<pre><code class="language-cpp">template&lt; char C &gt;
std::istream&amp; Charq( std::istream&amp; in )
{
    char c;
    if( in &gt;&gt; c &amp;&amp; c != C )
        in.setstate( std::ios_base::failbit );
    return in;
}

// own----------------------------------------------------------------------
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    using namespace std;
    typedef std::istream&amp; (*CharType)( std::istream&amp; in );
    CharType at = Charq&lt;','&gt;;
    int arr[3][3];
    ifstream file( &quot;input.txt&quot; );
    if( !file.is_open() )
    {
        cerr &lt;&lt; &quot;Fehler beim Oeffnen&quot; &lt;&lt; endl;
    }
    for( int iZl = 0; iZl &lt; 3
        &amp;&amp; file &gt;&gt; arr[iZl][0] &gt;&gt; at  &gt;&gt; arr[iZl][1] &gt;&gt; at  &gt;&gt; arr[iZl][2]; ++iZl )
        ;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1972466</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1972466</guid><dc:creator><![CDATA[Braunstein]]></dc:creator><pubDate>Fri, 29 Oct 2010 18:17:32 GMT</pubDate></item></channel></rss>