1

Write to the .txt file of the inheritance class problem (C ++)

 2 years ago
source link: https://www.codesd.com/item/write-to-the-txt-file-of-the-inheritance-class-problem-c.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Write to the .txt file of the inheritance class problem (C ++)

advertisements

Basically I am trying to save inputs from a data entry (Already been inputted by the user, just trying to print) to a .txt file. This is from a main class, and a child class, but it seems to just be saving the child classes's details...

I simplified the classes/int main in my example as my code is too long.

Any ideas how can I get it to save the 'vehicle' AND 'car' data entries in to the same document together?

Thanks

class vehicle {    

public:     

virtual void saveDetails() { 

    ofstream vehiclefile ("vehicle.txt");
    vehiclefile.open ("vehicle.txt");
    vehiclefile << "***Your Vehicle's Details***" << endl;
    vehiclefile << "Manufacturer:" << manufacturer << endl;
    vehiclefile << "Year of Manufacture:" << year << endl;
    vehiclefile << "Registration Number: " << regnum << endl;
    vehiclefile.close();
}

class lorry : public vehicle{

public:

  virtual void saveDetails() { 

    vehicle::saveDetails();

    ofstream vehiclefile;
    vehiclefile.open ("vehicle.txt");
    vehiclefile << "Car or Lorry: Lorry" << endl;
    vehiclefile << "Maximum weight: " << tonnage << endl;
    vehiclefile << "Body type: " << bodtype << endl;
    vehiclefile.close();
}

int main (); {

case '3':
        v -> saveDetails();
        break;
}


Ofstream.open takes a mode parameter. It looks like each successive write starts at the front of the file each time you open it. Try setting the mode to app (append) or ate (at end-though I'm not exactly certain on this behavior because I haven't tested and am posting from my phone)

vehiclefile.open("vehicle.txt", ios::out | ios::app); It looks like it's most likely overwriting all data because it starts at the beginning of the file and is not appending.

Remove the file name from the constructor in the base class as well, that's causing the file to open and there are no flags specified there... Even if you use them in the call to open you've already opened the file on the previous line.

 class vehicle {    

 public:     

 virtual void saveDetails() { 

ofstream vehiclefile;
vehiclefile.open ("vehicle.txt", ios::app|ios::out);
vehiclefile << "***Your Vehicle's Details***" << endl;
vehiclefile << "Manufacturer:" << manufacturer << endl;
vehiclefile << "Year of Manufacture:" << year << endl;
vehiclefile << "Registration Number: " << regnum << endl;
vehiclefile.close();
}

http://www.cplusplus.com/reference/fstream/ofstream/open/ has some info about the modes.

On the constructor, from link above:

Since the first task that is performed on a file stream is generally to open a file, these three classes include a constructor that automatically calls the open member function and has the exact same parameters as this member.

Remove the redundant file open and use the append open flag (in both clssses) and you should be good.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK