<?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[Probleme mit Dateien]]></title><description><![CDATA[<p>Hallo liebe C++-ler,</p>
<p>ich beschäftige mich gerade mit dem Speichern von Objekten in Dateien und habe dazu folgendes kleines Prohgramm geschrieben:<br />
[code]<br />
#include &lt;iostream&gt;<br />
#include &lt;fstream&gt;<br />
#include &lt;conio.h&gt;<br />
#include &lt;string&gt;<br />
using namespace std;<br />
/* run this program using the console pauser or add your own getch, system(&quot;pause&quot;) or input loop */<br />
class person<br />
{<br />
private:<br />
string name;<br />
int alter;</p>
<p>public:<br />
void getdata()<br />
{<br />
cout&lt;&lt; &quot;Name:&quot;;<br />
getline(cin,name);<br />
cout &lt;&lt; &quot;Alter&quot;;<br />
cin &gt;&gt; alter;<br />
fflush(stdin);<br />
}<br />
void showdata()<br />
{<br />
cout &lt;&lt; name &lt;&lt; &quot; &quot; &lt;&lt; alter &lt;&lt; endl;;<br />
}<br />
};<br />
int main(int argc, char *argv[])<br />
{<br />
person persfeld[3];<br />
person pers,pers2;<br />
fstream file;<br />
file.open(&quot;G:\\people.dat&quot;,ios::out|ios::trunc|ios::in|ios::binary);<br />
if(!file.good())<br />
cout&lt;&lt; &quot;Fehler bei Dateihandling&quot; &lt;&lt; endl;</p>
<p>for(int i =0;i&lt;3;i++)<br />
{<br />
persfeld[i].getdata();<br />
file.write((char*) &amp;persfeld[i],sizeof(person));<br />
if(!file.good())<br />
cout &lt;&lt; &quot;Fehler beim Schreiben!&quot;&lt;&lt;endl;<br />
}</p>
<p>file.close();<br />
// file.seekg(0,ios::beg);<br />
fstream file2;<br />
file2.open(&quot;G:\\people.dat&quot;,ios::in|ios::binary);<br />
if(!file2.good())<br />
cout&lt;&lt; &quot;Fehler bei Dateihandling (open)&quot; &lt;&lt; endl;</p>
<p>while(!file2.eof())<br />
{<br />
file2.read((char*)&amp;pers,sizeof(pers));<br />
if(!file2.good())<br />
cout &lt;&lt; &quot;Fehler beim Lesen!&quot; &lt;&lt;endl;<br />
else<br />
pers.showdata();</p>
<p>}<br />
file2.clear();<br />
file2.close();</p>
<p>return 0;<br />
}</p>
<p>Das Programm tut auch was es soll, nur nach der Ausgabe der Daten (Lesen der Datei) schmiert danach das Programm ab und ich finde den Fehler einfach nicht. Googeln hat leider bisher auch nichts ergeben. Könnt ihr mir vielleicht helfen?<br />
Ich arbeite mit dem DevC++ und bekomme die Meldung: Programm funktioniert nicht mehr.<br />
Vielen Dank schon mal.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/323978/probleme-mit-dateien</link><generator>RSS for Node</generator><lastBuildDate>Wed, 15 Jul 2026 09:07:42 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/323978.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 25 Feb 2014 09:34:33 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Probleme mit Dateien on Tue, 25 Feb 2014 09:34:33 GMT]]></title><description><![CDATA[<p>Hallo liebe C++-ler,</p>
<p>ich beschäftige mich gerade mit dem Speichern von Objekten in Dateien und habe dazu folgendes kleines Prohgramm geschrieben:<br />
[code]<br />
#include &lt;iostream&gt;<br />
#include &lt;fstream&gt;<br />
#include &lt;conio.h&gt;<br />
#include &lt;string&gt;<br />
using namespace std;<br />
/* run this program using the console pauser or add your own getch, system(&quot;pause&quot;) or input loop */<br />
class person<br />
{<br />
private:<br />
string name;<br />
int alter;</p>
<p>public:<br />
void getdata()<br />
{<br />
cout&lt;&lt; &quot;Name:&quot;;<br />
getline(cin,name);<br />
cout &lt;&lt; &quot;Alter&quot;;<br />
cin &gt;&gt; alter;<br />
fflush(stdin);<br />
}<br />
void showdata()<br />
{<br />
cout &lt;&lt; name &lt;&lt; &quot; &quot; &lt;&lt; alter &lt;&lt; endl;;<br />
}<br />
};<br />
int main(int argc, char *argv[])<br />
{<br />
person persfeld[3];<br />
person pers,pers2;<br />
fstream file;<br />
file.open(&quot;G:\\people.dat&quot;,ios::out|ios::trunc|ios::in|ios::binary);<br />
if(!file.good())<br />
cout&lt;&lt; &quot;Fehler bei Dateihandling&quot; &lt;&lt; endl;</p>
<p>for(int i =0;i&lt;3;i++)<br />
{<br />
persfeld[i].getdata();<br />
file.write((char*) &amp;persfeld[i],sizeof(person));<br />
if(!file.good())<br />
cout &lt;&lt; &quot;Fehler beim Schreiben!&quot;&lt;&lt;endl;<br />
}</p>
<p>file.close();<br />
// file.seekg(0,ios::beg);<br />
fstream file2;<br />
file2.open(&quot;G:\\people.dat&quot;,ios::in|ios::binary);<br />
if(!file2.good())<br />
cout&lt;&lt; &quot;Fehler bei Dateihandling (open)&quot; &lt;&lt; endl;</p>
<p>while(!file2.eof())<br />
{<br />
file2.read((char*)&amp;pers,sizeof(pers));<br />
if(!file2.good())<br />
cout &lt;&lt; &quot;Fehler beim Lesen!&quot; &lt;&lt;endl;<br />
else<br />
pers.showdata();</p>
<p>}<br />
file2.clear();<br />
file2.close();</p>
<p>return 0;<br />
}</p>
<p>Das Programm tut auch was es soll, nur nach der Ausgabe der Daten (Lesen der Datei) schmiert danach das Programm ab und ich finde den Fehler einfach nicht. Googeln hat leider bisher auch nichts ergeben. Könnt ihr mir vielleicht helfen?<br />
Ich arbeite mit dem DevC++ und bekomme die Meldung: Programm funktioniert nicht mehr.<br />
Vielen Dank schon mal.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2385131</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2385131</guid><dc:creator><![CDATA[turing2]]></dc:creator><pubDate>Tue, 25 Feb 2014 09:34:33 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit Dateien on Tue, 25 Feb 2014 10:03:39 GMT]]></title><description><![CDATA[<p>turing2 schrieb:</p>
<blockquote>
<p>ich beschäftige mich gerade mit dem Speichern von Objekten in Dateien und habe dazu folgendes kleines Prohgramm geschrieben:<br />
[...]</p>
</blockquote>
<p>Der Code hat sicherlich einige Unzulänglichkeiten, aber das größte Problem ist das <code>data member name</code> in <code>class person</code> .</p>
<p>Es ist implementierungsabhängig aber schematisch sieht std::string wohl meist so aus:</p>
<pre><code class="language-cpp">namespace std{
template&lt; blabla &gt;
class basic_string{
  CharT *data;
public:
  /* usw. usf. */
};
}
</code></pre>
<p>Der string hält die Daten über einen Zeiger. Und wenn Du so ein Objekt einfach in einen stream schreibst und wieder liest hast Du wahrscheinlich einen Fehler, weil Du nicht die Daten kopierst, sondern nur einen Zeiger (der dann auf meist weniger Sinnvolles zeigt).<br />
Stichworte: deep copy / shallow copy (tiefe Kopie / flache Kopie).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2385137</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2385137</guid><dc:creator><![CDATA[Furble Wurble]]></dc:creator><pubDate>Tue, 25 Feb 2014 10:03:39 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit Dateien on Tue, 25 Feb 2014 11:00:09 GMT]]></title><description><![CDATA[<p>Hi turing2,</p>
<p>Ausgabe auf die Konsole oder in einen Datei kann das selbe sein. Genauso ist in dem Fall Einlesen von Konsole und Einlesen von Datei das gleiche.</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;string&gt;

class person
{
private:
    std::string name;
    int alter;

public:
    //  showdata bzw. Speichern
    friend std::ostream&amp; operator&lt;&lt;( std::ostream&amp; out, const person&amp; p )
    {
        return out &lt;&lt; p.name &lt;&lt; &quot;\n&quot; &lt;&lt; p.alter &lt;&lt; std::endl;
    }
    // getdata (nur get-Teil)
    friend std::istream&amp; operator&gt;&gt;( std::istream&amp; in, person&amp; p )
    {
        return getline( in &gt;&gt; std::ws, p.name ) &gt;&gt; p.alter;
    }
};

using namespace std;
bool get_person_from_cin( person&amp; p )
{
    cout &lt;&lt; &quot;Name: &lt;NewLine&gt; Alter:\n&quot;;
    cin &gt;&gt; p;
    return cin.good();
}

int main() 
{
    {
        person persfeld[3];
        ofstream file(&quot;G:\\people.dat&quot;); // o -&gt; output
        if(!file.good())
            cout&lt;&lt; &quot;Fehler bei Dateihandling&quot; &lt;&lt; endl;

        for(int i =0;i&lt;3;i++)
        {
            if( get_person_from_cin( persfeld[i] ) )
            {
                file &lt;&lt; persfeld[i];
            }
        }
        if( !cin.good() )
            cout &lt;&lt; &quot;Fehler beim Lesen von cin&quot; &lt;&lt; endl;
        if(!file.good())
            cout &lt;&lt; &quot;Fehler beim Schreiben!&quot;&lt;&lt;endl;
    }   // ruft auch file.close()

    {
        ifstream file2(&quot;G:\\people.dat&quot;);   // i -&gt; input
        if(!file2.good())
            cout&lt;&lt; &quot;Fehler bei Dateihandling (open)&quot; &lt;&lt; endl;

        person pers;
        while( file2 &gt;&gt; pers )
        {
            cout &lt;&lt; pers;
        }
        if( !file2.eof() )
            cout &lt;&lt; &quot;Fehler beim Lesen!&quot; &lt;&lt;endl;
    }
    return 0;
}
</code></pre>
<p>Das Statement ' <code>while(!file2.eof())</code> ' ist ein Virus, der sich aus den Köpfen vieler über das gesamte Internet verbreitet hat. Falsch ist es trotzdem.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2385146</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2385146</guid><dc:creator><![CDATA[Werner_logoff]]></dc:creator><pubDate>Tue, 25 Feb 2014 11:00:09 GMT</pubDate></item></channel></rss>