I am a beginner in C++. I have input the values of the variables(belonging to a class) through an array of objects. Now how do I write them to the text file column wise as follows?? Thanks.........
SLNO NAME ADDRESS PHONE NO TYPE
1. ABC xyzagsgshsh 27438927 Mobile
2. QWE qwhjbbdh 78982338 Landline
This is my code for storing the data. How do I make it into a text file with contents as below?
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
class emp
{
string name,address,phone,type;
public:
void getdata();
}obj[5];
void emp::getdata()
{
cout<<"\nEnter the details:";
cout<<"\nName: ";cin>>name;
cout<<"Address:";
cin>>address;
cout<<"Phone number: "; cin>>phone;
cout<<"\nType of phone? (Landline/Mobile) :";
cin>>type;
}
int main()
{
ofstream ptr;
ptr.open("Phone.dat",ios::out);
cout<<"\nEnter the no.of.records: ";
int n,i;
cin>>n;
for(i=0;i<n;i++)
{
obj[i].getdata();
ptr.write((char*)&obj[i],sizeof(obj[i]));
}
return 0;
}