<?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[ofstream-Array per Schleife deklarieren, öffnen und verwenden]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>ich möchte in einem Programm mehrere txt-Dateien öffnen und in diese hineinschreiben. Dazu habe ich erstmal einen ofstream-Array deklariert:</p>
<pre><code>std::ofstream plot[10];
</code></pre>
<p>Aus diesem Array wollte ich nun jedes einzelne Feld öffnen:</p>
<pre><code>for(int i = 0; i &lt; 10; i++)     {
    plot[i].open(&quot;Plot%i.txt&quot;, i);
    plot[i].precision(15);
}
</code></pre>
<p>später verwenden:</p>
<pre><code>plot[i] &lt;&lt; ... &lt;&lt; ...;
</code></pre>
<p>und schließen:</p>
<pre><code>for(int i = 0; i &lt; 10; i++)     {
        plot[i].close();
}
</code></pre>
<p>Hatte mir schon gedacht dass das Öffnen nicht funktioniert, aber wie sonst?</p>
<p>Der Fehler hieß:<br />
&quot;error: invalid conversion from 'int' to std::ios_base::openmode&quot;</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/326599/ofstream-array-per-schleife-deklarieren-öffnen-und-verwenden</link><generator>RSS for Node</generator><lastBuildDate>Fri, 10 Jul 2026 20:56:43 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/326599.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 25 Jun 2014 19:00:19 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to ofstream-Array per Schleife deklarieren, öffnen und verwenden on Wed, 25 Jun 2014 19:00:19 GMT]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>ich möchte in einem Programm mehrere txt-Dateien öffnen und in diese hineinschreiben. Dazu habe ich erstmal einen ofstream-Array deklariert:</p>
<pre><code>std::ofstream plot[10];
</code></pre>
<p>Aus diesem Array wollte ich nun jedes einzelne Feld öffnen:</p>
<pre><code>for(int i = 0; i &lt; 10; i++)     {
    plot[i].open(&quot;Plot%i.txt&quot;, i);
    plot[i].precision(15);
}
</code></pre>
<p>später verwenden:</p>
<pre><code>plot[i] &lt;&lt; ... &lt;&lt; ...;
</code></pre>
<p>und schließen:</p>
<pre><code>for(int i = 0; i &lt; 10; i++)     {
        plot[i].close();
}
</code></pre>
<p>Hatte mir schon gedacht dass das Öffnen nicht funktioniert, aber wie sonst?</p>
<p>Der Fehler hieß:<br />
&quot;error: invalid conversion from 'int' to std::ios_base::openmode&quot;</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2405597</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2405597</guid><dc:creator><![CDATA[ihaveaquestion]]></dc:creator><pubDate>Wed, 25 Jun 2014 19:00:19 GMT</pubDate></item><item><title><![CDATA[Reply to ofstream-Array per Schleife deklarieren, öffnen und verwenden on Wed, 25 Jun 2014 19:06:55 GMT]]></title><description><![CDATA[<p>open ist nicht printf<sup>*</sup>.</p>
<pre><code>plot[i].open(&quot;Plot&quot; + to_string(i) + &quot;.txt&quot;);
</code></pre>
<p><sup>*</sup>: Und Programmieren ist nicht Ausprobieren. Ist verlockend, aber funktioniert nicht. Das dieser spezielle Fall ebenfalls nicht funktioniert, hättest du leicht mit einer Referenz zu open heraus finden können.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2405598</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2405598</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Wed, 25 Jun 2014 19:06:55 GMT</pubDate></item><item><title><![CDATA[Reply to ofstream-Array per Schleife deklarieren, öffnen und verwenden on Wed, 25 Jun 2014 19:10:11 GMT]]></title><description><![CDATA[<blockquote>
<p>ich möchte in einem Programm mehrere txt-Dateien öffnen und in diese hineinschreiben.</p>
</blockquote>
<p>Dann schreibe eine Schleife in der du in jedem Schleifendurchlauf einen ofstream erzeugst, initialisierst und in ihn die entsprechenden Daten schreibst. (Schließen ist nicht nötig, dass macht der Destruktor am Ende jedes Schleifendurchlaufs automatisch).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2405601</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2405601</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Wed, 25 Jun 2014 19:10:11 GMT</pubDate></item><item><title><![CDATA[Reply to ofstream-Array per Schleife deklarieren, öffnen und verwenden on Wed, 25 Jun 2014 19:12:58 GMT]]></title><description><![CDATA[<p>Danke für die schnelle Antwort! &quot;to_string was not declared in this scope&quot; brauch ich da noch eine andere Bibliothek via #include? Wie macht man hier Absätze?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2405603</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2405603</guid><dc:creator><![CDATA[ihaveaquestion]]></dc:creator><pubDate>Wed, 25 Jun 2014 19:12:58 GMT</pubDate></item><item><title><![CDATA[Reply to ofstream-Array per Schleife deklarieren, öffnen und verwenden on Wed, 25 Jun 2014 19:14:15 GMT]]></title><description><![CDATA[<p>An Arcoth: Das löst das Problem der Namensgebung nicht <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="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2405604</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2405604</guid><dc:creator><![CDATA[ihaveaquestion]]></dc:creator><pubDate>Wed, 25 Jun 2014 19:14:15 GMT</pubDate></item><item><title><![CDATA[Reply to ofstream-Array per Schleife deklarieren, öffnen und verwenden on Wed, 25 Jun 2014 19:17:53 GMT]]></title><description><![CDATA[<p>ihaveaquestion schrieb:</p>
<blockquote>
<p>An Arcoth: Das löst das Problem der Namensgebung nicht <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="😉"
    /></p>
</blockquote>
<pre><code>std::ostringstream stream;
stream &lt;&lt; &quot;Plot&quot; &lt;&lt; i &lt;&lt; &quot;.txt&quot;;
plot[i].open( stream.str().c_str() ); // Seit C++11 kannste das .c_str() weglassen
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2405605</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2405605</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Wed, 25 Jun 2014 19:17:53 GMT</pubDate></item><item><title><![CDATA[Reply to ofstream-Array per Schleife deklarieren, öffnen und verwenden on Wed, 25 Jun 2014 19:52:09 GMT]]></title><description><![CDATA[<p>Habe es jetzt so gemacht wie in deinem Code, mit Deklaration in der Schleife:</p>
<pre><code>for(int j = 0; j &lt; 10; j++)             {
    std::ostringstream stream;
    stream &lt;&lt; &quot;Plot&quot; &lt;&lt; j &lt;&lt; &quot;.txt&quot;;
    plot[j].open( stream.str() ); // Seit C++11 kannste das .c_str() weglassen
    plot.precision(15);
...
    plot[j] &lt;&lt; i &lt;&lt; &quot;   \t&quot; &lt;&lt; x &lt;&lt; &quot;   \t&quot; &lt;&lt; y &lt;&lt; &quot;   \t&quot; &lt;&lt; v &lt;&lt; &quot;   \t&quot; &lt;&lt; u &lt;&lt; &quot;   \n&quot;;
...
    plot[j].close();
</code></pre>
<p>Ich bekomme folgende Fehlermeldung: &quot;error: aggregate 'std::ostringstream stream' has incomplete type and cannot be defined&quot;</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2405609</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2405609</guid><dc:creator><![CDATA[ihaveaquestion]]></dc:creator><pubDate>Wed, 25 Jun 2014 19:52:09 GMT</pubDate></item><item><title><![CDATA[Reply to ofstream-Array per Schleife deklarieren, öffnen und verwenden on Wed, 25 Jun 2014 20:19:34 GMT]]></title><description><![CDATA[<p>Ich denke ich habe verstanden was passiert:</p>
<pre><code>std::ofstream plot;
    std::ostringstream stream;
    stream &lt;&lt; &quot;Plot&quot; &lt;&lt; j &lt;&lt; &quot;.txt&quot;;
    plot.open( stream.str().c_str() );
    plot.precision(15);
</code></pre>
<p>die Variable stream ist ein String und der wird zum öffnen benutzt, richtig? UND FUNKTIONIERT!! Danke! <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/2405612</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2405612</guid><dc:creator><![CDATA[ihaveaquestion]]></dc:creator><pubDate>Wed, 25 Jun 2014 20:19:34 GMT</pubDate></item><item><title><![CDATA[Reply to ofstream-Array per Schleife deklarieren, öffnen und verwenden on Wed, 25 Jun 2014 21:08:07 GMT]]></title><description><![CDATA[<p>ihaveaquestion schrieb:</p>
<blockquote>
<p>Danke für die schnelle Antwort! &quot;to_string was not declared in this scope&quot; brauch ich da noch eine andere Bibliothek via #include? Wie macht man hier Absätze?</p>
</blockquote>
<p>string brauchst du. Und es ist im namespace std. Und C++11 brauchst du, was aber seit ungefähr 3 Jahren Standard sein sollte <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="😉"
    /> . Du musst es wahrscheinlich nur aktivieren.</p>
<p>ihaveaquestion schrieb:</p>
<blockquote>
<p>die Variable stream ist ein String und der wird zum öffnen benutzt, richtig?</p>
</blockquote>
<p>Nein, es ist ein Stream <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="🙂"
    /> . Einer, der intern einen string als Puffer benutzt. Damit bastelst du dir hier einen passenden String für den Dateinamen.</p>
<p>Das ganze ist die urtümliche und etwas umständliche Art und Weise, Strings in C++ zu basteln. Du verstehst bei einem Vergleich zu meinem Vorschlag sicherlich, wieso man to_string eingeführt hat.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2405615</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2405615</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Wed, 25 Jun 2014 21:08:07 GMT</pubDate></item></channel></rss>