这里是一个程序,要求我将员工的数据保存在一个文本文件中,然后从该文件中读取数据,并找出ID为13的员工的薪水,我不知道如何读回我之前存储在文件中的相同变量,会有所帮助。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int size=3;
struct employees
{
int id;
string name;
int salary;
int age;
}emp[size];
int main()
{
ofstream file;
file.open("employee.txt",ios::out);
cout<<"\t------Enter data in the file------\n";
int count=1;
for(int i=0;i<size;i++)
{
cout<<"\tEnter info of employee "<<count<<endl;
cout<<"Enter the age:\n";
cin>>emp[i].age;
file<<"Age of the employee:\n";
file<<emp[i].age<<endl;
cout<<"Enter the name:\n";
cin>>emp[i].name;
file<<"Name of the employee:\n";
file<<emp[i].name<<endl;
cout<<"Enter the id:\n";
cin>>emp[i].id;
file<<"ID of the employee:\n";
file<<"ID= "<<emp[i].id<<endl;
cout<<"Enter the Salary:\n";
cin>>emp[i].salary;
file<<" Salary of the employee:\n";
file<<emp[i].salary<<endl;
count++;
}
file.close();
ifstream file2;
file2.open("employee.txt",ios::in);
//Here goes the reading part
file2.close();
return 0;
}