I am trying to make this program as my school project.
This program basically gets data from user store it in an object per and then saves it into a file PEARSON.TXT.
Now the problem is that i am not able to write the data into files I dont know why, but when i remove a few lines from it it runs but thats not what i want.
Also I observed that the last loop is also not doing well, i mean not running.
I know there is other ways to do this, but i have adopted this approach because i have to add more functions to that and have planned everything. Its just starting.
// FILE_IO.CPP
// Save the entered data into files and could be later viewed.
#include<fstream.h>
#include<conio.h>
#include<string.h>
class pearson
{
protected:
char name[40];
int age;
public:
void showdata();
void getdata();
void copydata(pearson);
};
void pearson::showdata()
{
cout << "\n\n\t\tName: " << name;
cout << "\n\t\tAge: " << age;
}
void pearson::getdata()
{
cout << "\n\t\tEnter name: "; cin >> name;
cout << "\t\tEnter age: "; cin >> age;
}
void pearson::copydata(pearson p)
{
strcpy(name, p.name);
age = p.age;
}
void main()
{
clrscr();
int s=0,l=0;
char ch='y';
pearson per, *per2[100];
fstream file;
file.open("PEARSON.TXT", ios::app | ios::in | ios::out);
file.seekg(0);
while(!file.eof())
{
per2[s] = new pearson;
file.read((char*)&per, sizeof(per));
per2[s]->copydata(per);
per2[s]->showdata();
s++;
}
l=s;
do
{
per.getdata();
per2[l] = new pearson;
per2[l]->copydata(per);
per2[l]->showdata();
// file.write((char*)&per, sizeof(per));
cout << "\n\t\tEnter Another: "; cin >> ch;
l++;
}
while(ch!='n');
cout << "\n\n\t\tAll entries are as follows: ";
file.seekg(0);
s=0;
while(s!=l)
{
per.copydata(*per2[s]);
file.write((char*)&per, sizeof(per));
s++;
cout << "\n" << s;
per2[s]->showdata();
}
s=0;
file.seekg(0);
while(!file.eof())
{
file.read((char*)&per, sizeof(per));
//per.showdata();
per2[s]->showdata();
s++;
}
file.close();
getch();
}