Daten speichern



  • Ich habe da so ein problem mit dem abspeichern von Variablen in Dateien.
    Hier erst einmal der Code.

    #include <iostream>
    #include <fstream>
    #include <string>
    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 << "Was möchten sie machen?\n";
    	cout << "1 - Daten schreiben.\n2 - Daten lesen\n";
    	cin >> choice;
    	switch (choice)
    	{
    	case 1: 
    			cout << "Wie alt ist die Katze\n";
    			cin >> quest;
    			Frisky->SetAge(quest);
    			cout << "Wie schwer ist die Katze?\n";
    			cin >> quest;
    			Frisky->SetWeight(quest);
    		        outFile.open("Text.txt", ios::out | ios::binary);
    		        outFile.write(reinterpret_cast<char *>(&Frisky), sizeof (Cat));
    			outFile.close();
    			break;
    	case 2: 
    			inFile.open("Text.txt", ios::in | ios::binary);
    			inFile.read(reinterpret_cast<char *>(&Frisky), sizeof (Cat));
    			cout << "Die Katze ist " << Frisky->GetAge() << " Jahre alt.\n";
    			cout << "Und " << Frisky->GetWeight() << " kg schwer.\n";
    			inFile.close();
    			break;
    	default: cout << "Falsche Eingabe!\n";
    	}
    	delete Frisky;
    	cin.sync();
    	cin.get();
    	return 0;
    }
    

    Der Fehler denk ich mir mal liegt im Konvertieren der Objekte in ein Format, welches zum schreiben in Dateien benötigt wird.
    Ich danke euch schon einmal im vorraus für die Antworten.



  • du hast es erkannt.
    So würde es gehen

    #include <fstream>
    #include <string>
    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 & operator << (const ostream & str, const Cat & thecat );
        friend istream & operator >> (istream & str, Cat & thecat );
    private:
        int itsWeight;
        int itsAge;
    };
    
    inline ostream & operator << ( ostream & str, const Cat & thecat ) {
       return str << thecat.GetWeight() << " " << thecat.GetAge() << " ";
    }
    inline istream & operator >> (istream & str, Cat & thecat ) {
       int age, weight;
       str >> weight >> age;
       thecat.SetAge(age);
       thecat.SetWeight(weight);
       return str;
    }
    
    int main()
    {
        Cat* Frisky = new Cat;
        short choice;
        short quest;
        ifstream inFile;
        ofstream outFile;
    
        cout << "Was möchten sie machen?\n";
        cout << "1 - Daten schreiben.\n2 - Daten lesen\n";
        cin >> choice;
        switch (choice)
        {
        case 1:
                cout << "Wie alt ist die Katze\n";
                cin >> quest;
                Frisky->SetAge(quest);
                cout << "Wie schwer ist die Katze?\n";
                cin >> quest;
                Frisky->SetWeight(quest);
                outFile.open("Text.txt", ios::out);
                outFile << *Frisky;
                outFile.close();
                break;
        case 2:
                inFile.open("Text.txt", ios::in );
    	    inFile >> *Frisky;
                cout << "Die Katze ist " << Frisky->GetAge() << " Jahre alt.\n";
                cout << "Und " << Frisky->GetWeight() << " kg schwer.\n";
                inFile.close();
                break;
        default: cout << "Falsche Eingabe!\n";
        }
        delete Frisky;
        cin.sync();
        cin.get();
        return 0;
    }
    

    Kurt


Anmelden zum Antworten