I have a vector of students and im trying to write them into a txt file. However, it will only write 1 student only. I also am not really sure on how to implement read file also. I got something in my mind but I;m not sure if it's correct or not.
calling write to file function from main
Code:
for(vector<clsStudent*>::const_iterator it = student.begin(); it != student.end();++it)
{
(*it)->writeToFile();
}
Code:
const char * filename = "studentinfo.txt";
void clsLocalStudent::writeToFile()
{
clsUniversityProgram *ptrProgram;
ptrProgram = getProgram();
ofstream out(filename);
if(out)
{
out << getName() << "\t"
<< getDateOfBirth() << "\t"
<< getGender() << "\t"
<< getNationality() << "\t"
<< getGPA() << "\t"
<< ptrProgram->getProgramName() << "\t"
<< ptrProgram->getProgramCode() << "\t"
<< ptrProgram->getProgramFees() << "\t"
<< getAccounts()->getAccountHolderName() << "\t"
<< getAccounts()->getAccountHolderID() << "\t"
<< getAccounts()->getAccountHolderDOB() << "\t"
<< getAccounts()->getAccountStartDate() << "\t"
<< getAccounts()->getPrincipal() << "\t"
<< getAccounts()->getYear() << "\t"
<< getAccounts()->getCI() << "\t"
<< getAccounts()->getC_Amount() << "\t"
<< calculateFees() << "\t"
<< getIC() << "\t"
<< getState();
out.close();
}
else
cerr << "\nCannot open " << filename << " for writing" << endl;
}
what i had in mind
Code:
void readFromFile(vector <clsStudent *> &s)
{
string name, double GPA;
ifstream in(filename);
if(in)
{
in >> name >> GPA;
in.close();
}
else
cerr << "\nCannot open " << filename << " for reading" << endl;
student = new clsLocalStudent(name, GPA); // parameterized constructor
s.push_back(student);
}