C++ reading text files
I have a program which I am writing which manages a savings club. I have set up a functions which creates members (there are two members types) and outputs there data to a text file a line at a time in the format string, int, int, date(name, account number, balance, date). I have also written a host of other functions which modify the balances and so on.
My question is, how would I go about loading this data in from a file for use in the program so that the data can be used in my functions. I would also like to know how you would search the text file with say using the account holders name and bring up all the assoiated associted account data (one line of this text file is one account). The below code is what creates the data within the file (albiet this has been shortend and altered so that it can work alone). I would like to extract that data so that I can calculate an individual members interest etc.
One other problem, the code below is a shorted version of the code for use in my main program. It outputs data to the file, but for some reason everytime I run it, it deletes whats in the file junmembers.txt and replaces it with the new data, even though I have loaded the file in append mode. Here is my code
#include <iostream>
#include <fstream>
#include <date>
using namespace std;
int main()
{
string a;
string n;
int b;
int aNum;
int date;
ofstream jun("file.txt", ios::app | ios::ate );
if (jun.fail ())
{
cout << "Error opening file, program closing" << endl;
exit(1);
}
cout << "Please enter a name" << endl;
cin >> n;
cout << "Please enter an age" << endl;
cin >> a;
cout << "Please enter an expiry date" << endl;
cin >> date;
srand ( time(NULL) ); // this seeds the random number so it will not be dulplicated
aNum = rand() % 999 + 1000;/* The random number generator here is producing a
random number between 0 and 999 and then will make the
random value be within the range of +1000 - +1999 as it is
addingg 1000. Accounts with numbers begining with 1 will be
Junior accounts and accounts with numbers beggining
with 2 will be Full accounts */
b = 0; //default balance will be nil or 0
jun << n << " " << aNum << " " << b << " " << a << " " << date << endl;//output to file
jun.close();
}