I'm having difficulty with this. I'm working on a project for a class that is an address book. The user will input some contact information which is stored into an object of a class, that object is then stored into an array. Once the user has finished inputting all their contacts into the program, it saves those contacts to a text file and exits the session. I am trying to read the saved data in from the text file, and store it back into the array. Here is how I save the data:
This is a member function of a class in the project, this class is used to first store data into the array, then sort it into alphabetical order, then search the contacts upon the user's request.
Code:
// This function will contact list to disk
void List::saveList()
{
ofstream contactDatabase;
contactDatabase.open("ContactDatabase.txt");
for (int x=0; x<nextEmptySpot; x++)
{
contactList[x].savePerson(contactDatabase);
}
contactDatabase.close();
}
That function calls a member function of the second class in my project. The second class asks the user for a contact's information, then stores it into private variables.
Code:
void Person::savePerson(ofstream& contactDatabase)
{
contactDatabase<<firstName<<endl<<lastName<<endl<<address<<endl<<zip<<endl<<phone<<endl<<email<<endl<<notes<<endl;
}
Now is where I am having trouble, trying to read in the saved data so that it can be searched and sorted with new data that the user might input. This is a global function in the main file.
Code:
void readFile()
{
List inputData;
ifstream contactDatabase("ContactDatabase.txt");
if (contactDatabase.is_open())
{
while (contactDatabase.good())
{
inputData.inputDataFromFile(contactDatabase);
}
contactDatabase.close();
}
else
cout<<"No existing contacts stored on disk\n\n";
}
That function calls a function intended to input data from the file back into the array. I can't seem to get this function correct. This is in our first class, since that is where my array is a private variable.
Code:
void List::inputDataFromFile(ifstream& contactDatabase)
{
string firstName;
contactDatabase.getline(contactDatabase, firstName);
contactList[nextEmptySpot] = firstName;
nextEmptySpot += 1;
}
I'm not sure if you will need to see other parts of the code or not, but this is what i have so far. I appreciate your feedback.