I'm writing a program to create and store profiles for a selected number of people. I've created a dynamic array to store the profiles (each of which is a struct containing 6 pieces of data).
My question is this: how would I go about storing the profiles in a text file for a kind of "save/load" operation? I've read about fstream operations but I'm not sure how to implement them in the case of dynamic arrays.
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
struct Profile
{
string name;
string hospitalOfBirth;
string city;
int dayOfBirth;
int monthOfBirth;
int yearOfBirth;
};
int main()
{
int profileNum = 0, i;
cout << "Enter the number of profiles: ";
(cin >> profileNum).ignore();
Profile* personProfile = new (nothrow) Profile[profileNum];
if (personProfile == 0)
cout << "Error: memory could not be allocated.";
else
{
for(i = 0; i < profileNum; i++)
{
cout << "Enter the name of this person: ";
getline(cin, personProfile[i].name);
cout << "Enter the hospital of this person's birth: ";
getline(cin, personProfile[i].hospitalOfBirth);
cout << "Enter the city of residence of this person: ";
getline(cin, personProfile[i].city);
cout << "Enter the day of birth of this person (DD): ";
cin >> personProfile[i].dayOfBirth;
cout << "Enter the month of birth of this person (MM): ";
cin >> personProfile[i].monthOfBirth;
cout << "Enter the year of birth of this person (YYYY): ";
cin >> personProfile[i].yearOfBirth;
cin.ignore();
cout << endl;
}
for(i = 0; i < profileNum; i++)
{
cout << "Name: " << personProfile[i].name << endl;
cout << "Hospital: " << personProfile[i].hospitalOfBirth << endl;
cout << "City: " << personProfile[i].city << endl;
cout << "Date of Birth: " << personProfile[i].dayOfBirth << "/" << personProfile[i].monthOfBirth << "/"
<< personProfile[i].yearOfBirth << endl;
cin.ignore();
}
}
delete [] personProfile;
cin.ignore();
return 0;
}