<?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[Daten speichern]]></title><description><![CDATA[<p>Ich habe da so ein problem mit dem abspeichern von Variablen in Dateien.<br />
Hier erst einmal der Code.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;string&gt;
using namespace std;

class Cat
{
public:
	Cat() {};
	~Cat() {};
	void SetWeight(int weight) {itsWeight = weight;}
	void SetAge(int age) {itsAge = age;}
	int GetWeight() const {return itsWeight;}
	int GetAge() const {return itsAge;}
private:
	int itsWeight;
	int itsAge;
};

int main()
{
	Cat* Frisky = new Cat;
	short choice;
	short quest;
	ifstream inFile;
	ofstream outFile;

	cout &lt;&lt; &quot;Was möchten sie machen?\n&quot;;
	cout &lt;&lt; &quot;1 - Daten schreiben.\n2 - Daten lesen\n&quot;;
	cin &gt;&gt; choice;
	switch (choice)
	{
	case 1: 
			cout &lt;&lt; &quot;Wie alt ist die Katze\n&quot;;
			cin &gt;&gt; quest;
			Frisky-&gt;SetAge(quest);
			cout &lt;&lt; &quot;Wie schwer ist die Katze?\n&quot;;
			cin &gt;&gt; quest;
			Frisky-&gt;SetWeight(quest);
		        outFile.open(&quot;Text.txt&quot;, ios::out | ios::binary);
		        outFile.write(reinterpret_cast&lt;char *&gt;(&amp;Frisky), sizeof (Cat));
			outFile.close();
			break;
	case 2: 
			inFile.open(&quot;Text.txt&quot;, ios::in | ios::binary);
			inFile.read(reinterpret_cast&lt;char *&gt;(&amp;Frisky), sizeof (Cat));
			cout &lt;&lt; &quot;Die Katze ist &quot; &lt;&lt; Frisky-&gt;GetAge() &lt;&lt; &quot; Jahre alt.\n&quot;;
			cout &lt;&lt; &quot;Und &quot; &lt;&lt; Frisky-&gt;GetWeight() &lt;&lt; &quot; kg schwer.\n&quot;;
			inFile.close();
			break;
	default: cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
	}
	delete Frisky;
	cin.sync();
	cin.get();
	return 0;
}
</code></pre>
<p>Der Fehler denk ich mir mal liegt im Konvertieren der Objekte in ein Format, welches zum schreiben in Dateien benötigt wird.<br />
Ich danke euch schon einmal im vorraus für die Antworten.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/129202/daten-speichern</link><generator>RSS for Node</generator><lastBuildDate>Wed, 26 Aug 2026 01:52:06 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/129202.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 11 Dec 2005 12:25:40 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Daten speichern on Sun, 11 Dec 2005 12:25:40 GMT]]></title><description><![CDATA[<p>Ich habe da so ein problem mit dem abspeichern von Variablen in Dateien.<br />
Hier erst einmal der Code.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;string&gt;
using namespace std;

class Cat
{
public:
	Cat() {};
	~Cat() {};
	void SetWeight(int weight) {itsWeight = weight;}
	void SetAge(int age) {itsAge = age;}
	int GetWeight() const {return itsWeight;}
	int GetAge() const {return itsAge;}
private:
	int itsWeight;
	int itsAge;
};

int main()
{
	Cat* Frisky = new Cat;
	short choice;
	short quest;
	ifstream inFile;
	ofstream outFile;

	cout &lt;&lt; &quot;Was möchten sie machen?\n&quot;;
	cout &lt;&lt; &quot;1 - Daten schreiben.\n2 - Daten lesen\n&quot;;
	cin &gt;&gt; choice;
	switch (choice)
	{
	case 1: 
			cout &lt;&lt; &quot;Wie alt ist die Katze\n&quot;;
			cin &gt;&gt; quest;
			Frisky-&gt;SetAge(quest);
			cout &lt;&lt; &quot;Wie schwer ist die Katze?\n&quot;;
			cin &gt;&gt; quest;
			Frisky-&gt;SetWeight(quest);
		        outFile.open(&quot;Text.txt&quot;, ios::out | ios::binary);
		        outFile.write(reinterpret_cast&lt;char *&gt;(&amp;Frisky), sizeof (Cat));
			outFile.close();
			break;
	case 2: 
			inFile.open(&quot;Text.txt&quot;, ios::in | ios::binary);
			inFile.read(reinterpret_cast&lt;char *&gt;(&amp;Frisky), sizeof (Cat));
			cout &lt;&lt; &quot;Die Katze ist &quot; &lt;&lt; Frisky-&gt;GetAge() &lt;&lt; &quot; Jahre alt.\n&quot;;
			cout &lt;&lt; &quot;Und &quot; &lt;&lt; Frisky-&gt;GetWeight() &lt;&lt; &quot; kg schwer.\n&quot;;
			inFile.close();
			break;
	default: cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
	}
	delete Frisky;
	cin.sync();
	cin.get();
	return 0;
}
</code></pre>
<p>Der Fehler denk ich mir mal liegt im Konvertieren der Objekte in ein Format, welches zum schreiben in Dateien benötigt wird.<br />
Ich danke euch schon einmal im vorraus für die Antworten.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/939507</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/939507</guid><dc:creator><![CDATA[Nap]]></dc:creator><pubDate>Sun, 11 Dec 2005 12:25:40 GMT</pubDate></item><item><title><![CDATA[Reply to Daten speichern on Sun, 11 Dec 2005 12:59:18 GMT]]></title><description><![CDATA[<p>du hast es erkannt.<br />
So würde es gehen</p>
<pre><code class="language-cpp">#include &lt;fstream&gt;
#include &lt;string&gt;
using namespace std;

class Cat
{
public:
    Cat() {};
    ~Cat() {};
    void SetWeight(int weight) {itsWeight = weight;}
    void SetAge(int age) {itsAge = age;}
    int GetWeight() const {return itsWeight;}
    int GetAge() const {return itsAge;}
    friend ostream &amp; operator &lt;&lt; (const ostream &amp; str, const Cat &amp; thecat );
    friend istream &amp; operator &gt;&gt; (istream &amp; str, Cat &amp; thecat );
private:
    int itsWeight;
    int itsAge;
};

inline ostream &amp; operator &lt;&lt; ( ostream &amp; str, const Cat &amp; thecat ) {
   return str &lt;&lt; thecat.GetWeight() &lt;&lt; &quot; &quot; &lt;&lt; thecat.GetAge() &lt;&lt; &quot; &quot;;
}
inline istream &amp; operator &gt;&gt; (istream &amp; str, Cat &amp; thecat ) {
   int age, weight;
   str &gt;&gt; weight &gt;&gt; age;
   thecat.SetAge(age);
   thecat.SetWeight(weight);
   return str;
}

int main()
{
    Cat* Frisky = new Cat;
    short choice;
    short quest;
    ifstream inFile;
    ofstream outFile;

    cout &lt;&lt; &quot;Was möchten sie machen?\n&quot;;
    cout &lt;&lt; &quot;1 - Daten schreiben.\n2 - Daten lesen\n&quot;;
    cin &gt;&gt; choice;
    switch (choice)
    {
    case 1:
            cout &lt;&lt; &quot;Wie alt ist die Katze\n&quot;;
            cin &gt;&gt; quest;
            Frisky-&gt;SetAge(quest);
            cout &lt;&lt; &quot;Wie schwer ist die Katze?\n&quot;;
            cin &gt;&gt; quest;
            Frisky-&gt;SetWeight(quest);
            outFile.open(&quot;Text.txt&quot;, ios::out);
            outFile &lt;&lt; *Frisky;
            outFile.close();
            break;
    case 2:
            inFile.open(&quot;Text.txt&quot;, ios::in );
	    inFile &gt;&gt; *Frisky;
            cout &lt;&lt; &quot;Die Katze ist &quot; &lt;&lt; Frisky-&gt;GetAge() &lt;&lt; &quot; Jahre alt.\n&quot;;
            cout &lt;&lt; &quot;Und &quot; &lt;&lt; Frisky-&gt;GetWeight() &lt;&lt; &quot; kg schwer.\n&quot;;
            inFile.close();
            break;
    default: cout &lt;&lt; &quot;Falsche Eingabe!\n&quot;;
    }
    delete Frisky;
    cin.sync();
    cin.get();
    return 0;
}
</code></pre>
<p>Kurt</p>
]]></description><link>https://www.c-plusplus.net/forum/post/939524</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/939524</guid><dc:creator><![CDATA[ZuK]]></dc:creator><pubDate>Sun, 11 Dec 2005 12:59:18 GMT</pubDate></item></channel></rss>