Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ saving/loading a vector to a file
    primarykey
    data
    text
    <p>I am creating a Calendar application in C++.</p> <p>Here is my code:</p> <pre><code>class appointment { public: appointment(string aDate, string aTime, string aType, string aLocation, string aComments, bool aIsImportant, string aReminderDate, string aReminderTime) { appDate = aDate; appTime = aTime; appType = aType; appLocation = aLocation; appComments = aComments; appIsImportant = aIsImportant; appReminderDate = aReminderDate; appReminderTime = aReminderTime; } void setDate(string aDate) { appDate = aDate; } void setTime(string aTime) { appTime = aTime; } void setType(string aType) { appType = aType; } void setLocation(string aLocation) { appLocation = aLocation; } void setComments(string aComments) { appComments = aComments; } void setIsImportant(bool aIsImportant) { appIsImportant = aIsImportant; } void setReminderDate(string aReminderDate) { appReminderDate = aReminderDate; } void setReminderTime(string aReminderTime) { appReminderTime = aReminderTime; } string getDate() { return appDate; } string getTime() { return appTime; } string getType() { return appType; } string getLocation() { return appLocation; } string getComments() { return appComments; } bool getIsImportant() { return appIsImportant; } string getReminderDate() { return appReminderDate; } string getReminderTime() { return appReminderTime; } private: appointment(); string appDate; string appTime; string appType; string appLocation; string appComments; bool appIsImportant; string appReminderDate; string appReminderTime; //person owner; }; class calendar { public: calendar() { loadFromFile(); } ~calendar() { saveToFile(); } void createAppointment(string aDate, string aTime, string aType, string aLocation, string aComments, bool aIsImportant, string aReminderDate, string aReminderTime) { appointment newAppointment(string aDate, string aTime, string aType, string aLocation, string aComments, bool aIsImportant, string aReminderDate, string aReminderTime); //appointments.resize(appointments.size() + 1,newAppointment); } private: vector&lt;appointment&gt; appointments; string calCurrentDate; string calCurrentTime; void loadFromFile() { //Code to load appointments from file } void saveToFile() { //Code to save appointments to file } }; </code></pre> <p>Can I please have some help with the following:</p> <p>When the constructor is called, I want to load a file (loadFromFile() method) of appointment objects and set the 'appointments' variable to have the contents of this file. After the saveToFile() method,I want to save the contents of the appointment vector to file.</p> <p>Also, when the createAppointment() method is called, I want to increase the size of the vector by 1, and add the contents to the vector. I am not sure of the correct code.</p> <p><strong>Update Saving/Loading from files</strong></p> <pre><code> void loadFromFile() { ifstream iStream("file.ext", ios::binary); fileHeader_t fHeader; iStream.read((char*)&amp;fHeader, sizeof(fileHeader_t)); if (fHeader.magicNumber = 0xDEADBEAF) { appointments.resize(fHeader.appointmentCount); iStream.read((char*)&amp;appointments[0], fHeader.appointmentCount * sizeof(appointment)); } } void saveToFile() { ofstream oStream("file.ext", ios::binary); fileHeader_t fHeader; fHeader.magicNumber = 0xDEADBEAF; fHeader.appointmentCount = appointments.size(); oStream.write((char*)&amp;fHeader, sizeof(fileHeader_t)); oStream.write((char*)&amp;appointments[0], sizeof(appointment) * appointments.size()); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload