<?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[Variabelnverbund (Struct) in Datei abspeichern]]></title><description><![CDATA[<p>Hallo liebe Gemeinde,</p>
<p>folgendes: Ich schreibe ein Programm, welches den Benutzer erstmal nach sämtlichen Daten ausfragt, d.h., Name, Telefon, Plz, etc.</p>
<p>Dafür habe ich ein StructDatentyp angelegt. Die Structvariable ist ein Array mit 10 Plätzen; ist es möglich, alle Parameter dieses Arrays (z.B. kd_daten[0].name, kd_daten[0].plz, ...) auf ein Schlag in eine Datei zu schieben?</p>
<p>Momentan mache ich es so, dass ich eine Funktion zum Abspeichern habe und die Funktion 4 mal aufrufe, d.h., für jeden einzelnen Parameter einmal - kleiner Auszug:</p>
<pre><code>// In VarKiste schieben
				auswahl[kto_position].name = input_name;
				save_file(pfad_daten, kto_position, input_name);

				int input_num;
				cout &lt;&lt; &quot;Plz: &quot;;
				cin &gt;&gt; input_num;
				auswahl[kto_position].plz = input_num;
				save_file(pfad_daten, kto_position, input_num);

				cout &lt;&lt; &quot;Tel: &quot;;
				cin &gt;&gt; input_num;
				auswahl[kto_position].tel_nr = input_num;
				save_file(pfad_daten, kto_position, input_num);
</code></pre>
<p>...und die Funktion:</p>
<pre><code>void save_file(string pfad, int pos_kd, string data_kd)
{
	string files_all;

	if (pos_kd != -1)
	{
		files_all += pos_kd+'0';
	}
	else
	{
		files_all += '\n';
	}

	files_all += data_kd;

	// Index schreiben
	kto_info.open(&quot;c:/TEST/info.txt&quot;, ios::out);
	kto_info &lt;&lt; files_all;
	kto_info.close();
}
</code></pre>
<p>(Der Pfadparameter der Fkt. ist noch überflüssig, ich weiß)</p>
<p>Geht das vlt. irgendwie eleganter? Irgendwie ist das doof so....</p>
<p>Gruß</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/289879/variabelnverbund-struct-in-datei-abspeichern</link><generator>RSS for Node</generator><lastBuildDate>Tue, 18 Aug 2026 21:46:10 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/289879.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 14 Jul 2011 18:59:08 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Variabelnverbund (Struct) in Datei abspeichern on Thu, 14 Jul 2011 18:59:52 GMT]]></title><description><![CDATA[<p>Hallo liebe Gemeinde,</p>
<p>folgendes: Ich schreibe ein Programm, welches den Benutzer erstmal nach sämtlichen Daten ausfragt, d.h., Name, Telefon, Plz, etc.</p>
<p>Dafür habe ich ein StructDatentyp angelegt. Die Structvariable ist ein Array mit 10 Plätzen; ist es möglich, alle Parameter dieses Arrays (z.B. kd_daten[0].name, kd_daten[0].plz, ...) auf ein Schlag in eine Datei zu schieben?</p>
<p>Momentan mache ich es so, dass ich eine Funktion zum Abspeichern habe und die Funktion 4 mal aufrufe, d.h., für jeden einzelnen Parameter einmal - kleiner Auszug:</p>
<pre><code>// In VarKiste schieben
				auswahl[kto_position].name = input_name;
				save_file(pfad_daten, kto_position, input_name);

				int input_num;
				cout &lt;&lt; &quot;Plz: &quot;;
				cin &gt;&gt; input_num;
				auswahl[kto_position].plz = input_num;
				save_file(pfad_daten, kto_position, input_num);

				cout &lt;&lt; &quot;Tel: &quot;;
				cin &gt;&gt; input_num;
				auswahl[kto_position].tel_nr = input_num;
				save_file(pfad_daten, kto_position, input_num);
</code></pre>
<p>...und die Funktion:</p>
<pre><code>void save_file(string pfad, int pos_kd, string data_kd)
{
	string files_all;

	if (pos_kd != -1)
	{
		files_all += pos_kd+'0';
	}
	else
	{
		files_all += '\n';
	}

	files_all += data_kd;

	// Index schreiben
	kto_info.open(&quot;c:/TEST/info.txt&quot;, ios::out);
	kto_info &lt;&lt; files_all;
	kto_info.close();
}
</code></pre>
<p>(Der Pfadparameter der Fkt. ist noch überflüssig, ich weiß)</p>
<p>Geht das vlt. irgendwie eleganter? Irgendwie ist das doof so....</p>
<p>Gruß</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2092861</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2092861</guid><dc:creator><![CDATA[wolfbiker]]></dc:creator><pubDate>Thu, 14 Jul 2011 18:59:52 GMT</pubDate></item><item><title><![CDATA[Reply to Variabelnverbund (Struct) in Datei abspeichern on Thu, 14 Jul 2011 20:01:16 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">#include &lt;string&gt;
#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;vector&gt;

using namespace std;

struct data
{
  string name;
  string telefonnummer;
  string postleitzahl;
};

ostream&amp; operator&lt;&lt;(ostream &amp;out, data const &amp;rhs)
{
  out &lt;&lt; &quot;Name: &quot; &lt;&lt; rhs.name &lt;&lt; '\n'
      &lt;&lt; &quot;Tel.: &quot; &lt;&lt; rhs.telefonnummer &lt;&lt; '\n'
      &lt;&lt; &quot;Plz.: &quot; &lt;&lt; rhs.postleitzahl &lt;&lt; '\n';
  return out;
}

istream&amp; operator&gt;&gt;(istream &amp;in, data &amp;rhs)
{
  cout &lt;&lt; &quot;Name? &quot;;
  in &gt;&gt; rhs.name;
  cout &lt;&lt; &quot;Telefonnummer? &quot;;
  in &gt;&gt; rhs.telefonnummer;
  cout &lt;&lt; &quot;Postleitzahl? &quot;;
  in &gt;&gt; rhs.postleitzahl;
  return in;
}

int main()
{
  vector&lt;data&gt; daten;

  {
    cout &lt;&lt; &quot;Datensätze eingeben. Zum Beenden CTRL+Z (Win/DOS) oder CTRL+D (Mac/Unix)\n\n&quot;;
    data input;
    while (cin &gt;&gt; input)
      daten.push_back(input);
  }

  {
    ofstream out(&quot;info.txt&quot;);
    for (size_t i = 0; i &lt; daten.size(); ++i)
      out &lt;&lt; daten[i] &lt;&lt; '\n';
  }
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2092887</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2092887</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Thu, 14 Jul 2011 20:01:16 GMT</pubDate></item><item><title><![CDATA[Reply to Variabelnverbund (Struct) in Datei abspeichern on Fri, 15 Jul 2011 17:32:18 GMT]]></title><description><![CDATA[<p>Hallo Sepp,</p>
<p>dankeschön erstmal für deine Antwort!</p>
<p>Das sieht ja schon mal ein gewaltiges Stück komplizierter aus - ich arbeite das heute Abend mal durch. Hoffentlich komm ich dahinter. Auf jeden Fall tuts das, was ich mir erhoffe; melde mich dann bzgl. Fragen.</p>
<p>Gruß</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2093287</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2093287</guid><dc:creator><![CDATA[wolfbiker]]></dc:creator><pubDate>Fri, 15 Jul 2011 17:32:18 GMT</pubDate></item><item><title><![CDATA[Reply to Variabelnverbund (Struct) in Datei abspeichern on Fri, 15 Jul 2011 17:37:57 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/19375">@SeppJ</a>: Meinst du wirklich es ist so günstig, wenn der Input-Operator nebenbei nach cout schreibt? Davon abgesehen würde ich die Operatoren symmetrisch aufbauen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2093291</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2093291</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Fri, 15 Jul 2011 17:37:57 GMT</pubDate></item><item><title><![CDATA[Reply to Variabelnverbund (Struct) in Datei abspeichern on Fri, 15 Jul 2011 17:50:16 GMT]]></title><description><![CDATA[<p>CStoll schrieb:</p>
<blockquote>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/19375">@SeppJ</a>: Meinst du wirklich es ist so günstig, wenn der Input-Operator nebenbei nach cout schreibt? Davon abgesehen würde ich die Operatoren symmetrisch aufbauen.</p>
</blockquote>
<p>Nein, das halte ich nicht für günstig, aber für lehrreich, damit er überhaupt mal Operatorüberladung sieht und das Programm einigermaßen gut aussieht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2093301</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2093301</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Fri, 15 Jul 2011 17:50:16 GMT</pubDate></item></channel></rss>