<?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[Schreiben von Werten in eine Textdatei mit einer Funktion]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe bereits folgendes probiert:</p>
<pre><code class="language-cpp">//testofmain.cpp
#include &lt;iostream&gt;
#include &lt;cmath&gt;
#include &lt;fstream&gt;

using namespace std;

void TextToTxt(double a,double b);

void main()
{
	ofstream ausgabe(&quot;ausgabe.txt&quot;);
	ausgabe &lt;&lt; &quot;Hier steht die includeanweisung (darf nur einmal in die Datei)&quot; &lt;&lt; endl;

	double a, b;

	for(int i=0; i&lt;4; i++)
	{
		a+=5;
		b+=10;
		void TextToTxt(a,b);
	}

}
</code></pre>
<p>und hier die Funktion:</p>
<pre><code class="language-cpp">//functiontest.cpp

#include &lt;iostream&gt;
#include &lt;cmath&gt;
#include &lt;fstream&gt;

using namespace std;

//Funktion zum Anhängen an die Textdatei
void TextToTxt(double a,double b)
{
	ofstream ausgabe(&quot;ausgabe.txt&quot;);
	ausgabe &lt;&lt; &quot;\nWerte für a: &quot; &lt;&lt; a &lt;&lt; endl;
	ausgabe &lt;&lt; &quot;\nWerte für b: &quot; &lt;&lt; b &lt;&lt; endl;
}
</code></pre>
<p>dabei will ich beliebig viel und vor allem flexibel Werte an diese Datei anhängen. Hab auch schon überlegt, alles in einen String umzuwandeln ( in der Funktion) und dann einfach direkt als Text an die Textdatei zu übergeben ...<br />
Hat jemand einen Tip?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/184536/schreiben-von-werten-in-eine-textdatei-mit-einer-funktion</link><generator>RSS for Node</generator><lastBuildDate>Wed, 23 Sep 2026 19:52:22 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/184536.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 16 Jun 2007 11:37:27 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Schreiben von Werten in eine Textdatei mit einer Funktion on Sat, 16 Jun 2007 11:37:27 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe bereits folgendes probiert:</p>
<pre><code class="language-cpp">//testofmain.cpp
#include &lt;iostream&gt;
#include &lt;cmath&gt;
#include &lt;fstream&gt;

using namespace std;

void TextToTxt(double a,double b);

void main()
{
	ofstream ausgabe(&quot;ausgabe.txt&quot;);
	ausgabe &lt;&lt; &quot;Hier steht die includeanweisung (darf nur einmal in die Datei)&quot; &lt;&lt; endl;

	double a, b;

	for(int i=0; i&lt;4; i++)
	{
		a+=5;
		b+=10;
		void TextToTxt(a,b);
	}

}
</code></pre>
<p>und hier die Funktion:</p>
<pre><code class="language-cpp">//functiontest.cpp

#include &lt;iostream&gt;
#include &lt;cmath&gt;
#include &lt;fstream&gt;

using namespace std;

//Funktion zum Anhängen an die Textdatei
void TextToTxt(double a,double b)
{
	ofstream ausgabe(&quot;ausgabe.txt&quot;);
	ausgabe &lt;&lt; &quot;\nWerte für a: &quot; &lt;&lt; a &lt;&lt; endl;
	ausgabe &lt;&lt; &quot;\nWerte für b: &quot; &lt;&lt; b &lt;&lt; endl;
}
</code></pre>
<p>dabei will ich beliebig viel und vor allem flexibel Werte an diese Datei anhängen. Hab auch schon überlegt, alles in einen String umzuwandeln ( in der Funktion) und dann einfach direkt als Text an die Textdatei zu übergeben ...<br />
Hat jemand einen Tip?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1307140</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1307140</guid><dc:creator><![CDATA[Stephan_lernt_C++]]></dc:creator><pubDate>Sat, 16 Jun 2007 11:37:27 GMT</pubDate></item><item><title><![CDATA[Reply to Schreiben von Werten in eine Textdatei mit einer Funktion on Sat, 16 Jun 2007 11:52:08 GMT]]></title><description><![CDATA[<p>versteh ehrlich gesagt das problem nicht. soll das eine art konfigurationsdatei werden, in der für bestimmte variablen werte festgelegt werden?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1307150</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1307150</guid><dc:creator><![CDATA[thordk]]></dc:creator><pubDate>Sat, 16 Jun 2007 11:52:08 GMT</pubDate></item><item><title><![CDATA[Reply to Schreiben von Werten in eine Textdatei mit einer Funktion on Sat, 16 Jun 2007 12:09:15 GMT]]></title><description><![CDATA[<p>nein, man kann sich das so ungefähr vorstellen, wie wenn ich zum Beispiel eine Vrml - Datei schreiben will. Dann muss ich am Anfang der Datei &quot;#VRML V2.0 utf8&quot; eingeben.<br />
Jedoch will ich dann immer weiter in diese Datei schreiben und möchte dazu eine Funktion benutzen. Ist das möglich?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1307157</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1307157</guid><dc:creator><![CDATA[Stephan_lernt_C++]]></dc:creator><pubDate>Sat, 16 Jun 2007 12:09:15 GMT</pubDate></item><item><title><![CDATA[Reply to Schreiben von Werten in eine Textdatei mit einer Funktion on Sat, 16 Jun 2007 12:10:41 GMT]]></title><description><![CDATA[<p>Da wir schon (in nem andren Thread) die Diskussion über Anfänger und den Standard hatten, noch ein Hinweis:</p>
<p>Statt:</p>
<pre><code class="language-cpp">void main()
{}
</code></pre>
<p>bitte:</p>
<pre><code class="language-cpp">int main()
{}
</code></pre>
<blockquote>
<p>Jedoch will ich dann immer weiter in diese Datei schreiben und möchte dazu eine Funktion benutzen. Ist das möglich?</p>
</blockquote>
<p>Klar ist das möglich. Du musst in der Funktion nur Zugriff auf den Stream haben, oder den Stream neu öffnen mit der Option das der Inhalt nicht gelöscht wird.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1307158</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1307158</guid><dc:creator><![CDATA[David_pb]]></dc:creator><pubDate>Sat, 16 Jun 2007 12:10:41 GMT</pubDate></item><item><title><![CDATA[Reply to Schreiben von Werten in eine Textdatei mit einer Funktion on Sat, 16 Jun 2007 12:36:25 GMT]]></title><description><![CDATA[<blockquote>
<p>Klar ist das möglich. Du musst in der Funktion nur Zugriff auf den Stream haben, oder den Stream neu öffnen mit der Option das der Inhalt nicht gelöscht wird</p>
</blockquote>
<p>Das freut mich, das es möglich ist. Aber wie kann ich das machen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1307178</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1307178</guid><dc:creator><![CDATA[Stephan_lernt_C++]]></dc:creator><pubDate>Sat, 16 Jun 2007 12:36:25 GMT</pubDate></item><item><title><![CDATA[Reply to Schreiben von Werten in eine Textdatei mit einer Funktion on Sat, 16 Jun 2007 12:38:42 GMT]]></title><description><![CDATA[<p>Sagte ich das nicht bereits?</p>
<blockquote>
<p>**<br />
Du musst in der Funktion nur Zugriff auf den Stream haben, oder den Stream neu öffnen mit der Option das der Inhalt**</p>
</blockquote>
<p>Also, Streamobjekt übergeben (per Referenz) oder halt mit dem Flag std::ios::app öffnen. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f644.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_rolling_eyes"
      title=":rolling_eyes:"
      alt="🙄"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1307181</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1307181</guid><dc:creator><![CDATA[David_pb]]></dc:creator><pubDate>Sat, 16 Jun 2007 12:38:42 GMT</pubDate></item><item><title><![CDATA[Reply to Schreiben von Werten in eine Textdatei mit einer Funktion on Sat, 16 Jun 2007 12:45:04 GMT]]></title><description><![CDATA[<p>Das versteh ich leider nicht ... könntest du nicht ein Beispiel zeigen, wie du das in der Funktion machst und diese dann aufrufst? <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>Bin leider noch ein Anfänger ...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1307185</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1307185</guid><dc:creator><![CDATA[Stephan_lernt_C++]]></dc:creator><pubDate>Sat, 16 Jun 2007 12:45:04 GMT</pubDate></item><item><title><![CDATA[Reply to Schreiben von Werten in eine Textdatei mit einer Funktion on Sat, 16 Jun 2007 12:49:58 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">#include &lt;string&gt;
#include &lt;fstream&gt;

void TextToTxt( double a, double b, std::ofstream&amp; stream )
{
	stream &lt;&lt; &quot;Wert fuer a: &quot; &lt;&lt; a &lt;&lt; '\n';
	stream &lt;&lt; &quot;Wert fuer b: &quot; &lt;&lt; b &lt;&lt; '\n';
}

int main()
{
	std::ofstream ausgabe( &quot;ausgabe.txt&quot; );
	ausgabe &lt;&lt; &quot;foobar\n&quot;;

	double a, b;

	a = b = 0.0;

	for ( int i = 0; i &lt; 4; ++i )
	{
		a += 5;
		b += 10;
		TextToTxt( a, b, ausgabe );
	}

	ausgabe.close();
	std::cin.get();
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1307188</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1307188</guid><dc:creator><![CDATA[David_pb]]></dc:creator><pubDate>Sat, 16 Jun 2007 12:49:58 GMT</pubDate></item><item><title><![CDATA[Reply to Schreiben von Werten in eine Textdatei mit einer Funktion on Sat, 16 Jun 2007 12:55:52 GMT]]></title><description><![CDATA[<p>In etwa so...:</p>
<pre><code class="language-cpp">void TextToTxt(std::ofstream&amp; ofTxt, double a, double b)
{
    if(!ofTxt.is_open())
       return; // ggfs. auch mit asssert, oder Du legst die Methode gleich, wie von David_pb vorgeschlagen so aus, dass sie die Datei selbst öffnet.
    ofTxt &lt;&lt; &quot;\nWerte für a: &quot; &lt;&lt; a &lt;&lt; std::endl;
    ofTxt &lt;&lt; &quot;\nWerte für b: &quot; &lt;&lt; b &lt;&lt; std::endl;
}
</code></pre>
<p>Benutzung dann so:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;fstream&gt;

void TextToTxt(std::ofstream&amp;,double,double);

int main()
{
   using namespace std;

   ofstream ofTextFile(&quot;ausgabe.txt&quot;);
   TextToTxt(ofTextFile, 34.45, 12.987);

   // [...]

   return (0);
}

// Definition von TextToTxt dann hier...
</code></pre>
<p>Edit: Tüdeldüü...jaja, too-late :p .</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1307190</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1307190</guid><dc:creator><![CDATA[CodeFinder]]></dc:creator><pubDate>Sat, 16 Jun 2007 12:55:52 GMT</pubDate></item><item><title><![CDATA[Reply to Schreiben von Werten in eine Textdatei mit einer Funktion on Sat, 16 Jun 2007 13:24:37 GMT]]></title><description><![CDATA[<p>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>
<p>Jetzt klappt es <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="🙂"
    /> ich werde weiter üben um irgendwann mal genau so gut zu sein!!!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1307205</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1307205</guid><dc:creator><![CDATA[Stephan_lernt_C++]]></dc:creator><pubDate>Sat, 16 Jun 2007 13:24:37 GMT</pubDate></item><item><title><![CDATA[Reply to Schreiben von Werten in eine Textdatei mit einer Funktion on Sat, 16 Jun 2007 14:28:18 GMT]]></title><description><![CDATA[<p>Nimm besser <code>std::ostream</code> als Parametertyp für <code>TextToTxt</code> (und überleg dir vielleicht nochmal ob dir nicht ein besserer Bezeichner einfällt).</p>
<p>CodeFinder schrieb:</p>
<blockquote>
<pre><code class="language-cpp">if(!ofTxt.is_open())
       return; // ggfs. auch mit asssert, oder Du legst die Methode gleich, wie von David_pb vorgeschlagen so aus, dass sie die Datei selbst öffnet.
</code></pre>
</blockquote>
<p>Lass das lieber wie in David_pbs Code und prüfe einfach direkt nach dem öffnen ob alles ok ist:</p>
<pre><code class="language-cpp">string ausgabe_pfad = &quot;ausgabe.txt&quot;;
ofstream ausgabe(ausgabe_pfad.c_str());
if (!ausgabe) {
  cerr &lt;&lt; &quot;Datei \&quot;&quot; &lt;&lt; ausgabe_pfad &lt;&lt; &quot;\&quot; konnte nicht geoeffnet werden&quot; &lt;&lt; endl;
  return EXIT_FAILURE;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1307251</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1307251</guid><dc:creator><![CDATA[finix]]></dc:creator><pubDate>Sat, 16 Jun 2007 14:28:18 GMT</pubDate></item></channel></rss>