能不能帮我看一下下面的代码错在哪里 #include <iostream> #include <fstream> #include <string> using n
能不能帮我看一下下面的代码错在哪里#include<iostream>#include<fstream>#include<string>usingnamespacestd...
能不能帮我看一下下面的代码错在哪里
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Salary
{
public:
string id;
int salary;
Salary(string ID, int S)
{
id = ID;
salary = S;
}
Salary(){};
void Save();
void Load();
};
void Salary ::Save()
{
Salary em ("6001", 4000);
Salary em2("43098", 2938);
ofstream os("data.txt", ios_base::out|ios_base::binary );
os.write(reinterpret_cast<char*>(&em), sizeof(em));
os.write(reinterpret_cast<char*>(&em2), sizeof(em2));
os.close();
}
void Salary::Load()
{
ifstream is("data.txt", ios_base::in|ios_base::binary);
if(is)
{
while(is)
{
Salary emp ;
is.read(reinterpret_cast<char*>(&emp), sizeof(emp));
cout << emp.id << " " << emp.salary << endl;
}
}
else
{
cout << "can not open" << endl;
}
}
int main()
{
Salary A;
A.Save();
A.Load();
return 0;
} 展开
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Salary
{
public:
string id;
int salary;
Salary(string ID, int S)
{
id = ID;
salary = S;
}
Salary(){};
void Save();
void Load();
};
void Salary ::Save()
{
Salary em ("6001", 4000);
Salary em2("43098", 2938);
ofstream os("data.txt", ios_base::out|ios_base::binary );
os.write(reinterpret_cast<char*>(&em), sizeof(em));
os.write(reinterpret_cast<char*>(&em2), sizeof(em2));
os.close();
}
void Salary::Load()
{
ifstream is("data.txt", ios_base::in|ios_base::binary);
if(is)
{
while(is)
{
Salary emp ;
is.read(reinterpret_cast<char*>(&emp), sizeof(emp));
cout << emp.id << " " << emp.salary << endl;
}
}
else
{
cout << "can not open" << endl;
}
}
int main()
{
Salary A;
A.Save();
A.Load();
return 0;
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询