我是 C++ 的初学者。我已经通过一个对象数组输入了变量的值(属于一个类)。现在我如何将它们写入文本文件列,如下所示?谢谢.........
SLNO NAME ADDRESS PHONE NO TYPE
1. ABC xyzagsgshsh 27438927 Mobile
2. QWE qwhjbbdh 78982338 Landline
这是我存储数据的代码。如何将其制作成内容如下的文本文件?
#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;
}