信息管理
#include<iostream>#include<fstream>
using namespace std;
struct staff
{
int num;
char name[20];
int age;
double pay;
};
int main()
{
staff staff[7]={2101,"li",34,1203,2104,"wang",23,674.5,2108,"fun",54,778,3006,"xue",45,476.5,5101,"ling",39,656.6
},staff1;
fstream iofile("staff.dat",ios::in|ios::out|ios::binary);
if(!iofile)
{
cerr<<"open staff.dat error!"<<endl;
exit(1);
}
int i,m,num;
cout<<"Five staff:"<<endl;
for(i=0;i<5;i++)
{
cout<<staff[i].num<<staff[i].name<<staff[i].age<<staff[i].pay<<endl;
iofile.write((char *)&staff[i],sizeof(staff[i]));
}
cout<<"Please input data you want insert:"<<endl;
iofile.seekp(0,ios::end);
for(i=0;i<2;i++)
{
cin>>staff1.num>>staff1.name>>staff1.age>>staff1.pay;
iofile.write((char *)&staff1,sizeof(staff1));
}
cout<<"Senven staff:"<<endl;
iofile.seekg(0,ios::beg);
for(i=0;i<7;i++)
{
iofile.read((char *)&staff[i],sizeof(staff[i]));
cout<<staff[i].num<<staff[i].name<<staff[i].age<<staff[i].pay<<endl;
}
bool find;
cout<<"enter number you want search ,enter 0 to stop"<<endl;
cin>>num;
while(num)
{
find=false;
iofile.seekg(0,ios::beg);
for(i=0;i<7;i++)
{
iofile.read((char *)&staff[i],sizeof(staff[i]));
if(num==staff[i].num)
{
m=iofile.tellg();
cout<<num<<"is No."<<m/sizeof(staff1)<<endl;
cout<<staff[i].num<<""<<staff[i].name<<""<<staff[i].age<<""<<staff[i].pay<<endl;
find=true;
break;
}
}
if(!find)
cout<<"cant't find"<<num<<endl;
cout<<"enter number you want search,enter 0 to stop.";
cin>>num;
}
iofile.close();
return 0;
}
为什么老是出现
open staff.dat error!
Press any key to continue一直解决不了,求帮忙