#include <iostream>
#include <fstream>
using namespace std;
struct staff
{int num;
charname[20];
intage;
double pay;
};
int main()
{staffstaf[7]={2101,"Li",34,1203,2104,"Wang",23,674.5,2108,"Fun",54,778,
3006,"Xue",45,476.5,5101,"Ling",39,656.6},staf1;
fstreamiofile("staff.dat",ios::in|ios::out|ios::binary);
if(!iofile)
{cerr<<"open error!"<<endl;
abort();
}
inti,m,num;
cout<<"Five staff:"<<endl;
for(i=0;i<5;i++)
{cout<<staf[i].num<<""<<staf[i].name<<""<<staf[i].age<<" "<<staf[i].pay<<endl;
iofile.write((char *)&staf[i],sizeof(staf[i]));}
cout<<"please input data you wantinsert:"<<endl;
for(i=0;i<2;i++)
{cin>>staf1.num>>staf1.name>>staf1.age>>staf1.pay;
iofile.seekp(0,ios::end);
iofile.write((char *)&staf1,sizeof(staf1));}
iofile.seekg(0,ios::beg);
for(i=0;i<7;i++)
{iofile.read((char *)&staf[i],sizeof(staf[i]));
cout<<staf[i].num<<""<<staf[i].name<<""<<staf[i].age<<" "<<staf[i].pay<<endl;
}
boolfind;
cout<<"enter number you wantsearch,enter 0 to stop.";
cin>>num;
while(num)
{find=false;
iofile.seekg(0,ios::beg);
for(i=0;i<7;i++)
{iofile.read((char *)&staf[i],sizeof(staf[i]));
if(num==staf[i].num)
{m=iofile.tellg();
cout<<num<<" isNo."<<m/sizeof(staf1)<<endl;
cout<<staf[i].num<<""<<staf[i].name<<""<<staf[i].age<<" "<<staf[i].pay<<endl;
find=true;
break;
}
}
if(!find)
cout<<"can't find "<<num<<endl;
cout<<"enter number you want search,enter 0 to stop.";
cin>>num;
}
iofile.close();
return 0;
}
n�>��*�*s=MsoNormal> exit(1);
}
ofstream outfile("f2.dat",ios::app);
//f2.dat作为输出文件,文件指针指向文件尾,向它写入的数据放在原来数据的后面
if(!outfile)
{cerr<<"open f2.dat error!"<<endl;
exit(1);
}
inta;
for(int i=0;i<10;i++)
{infile>>a; //磁盘文件f2.dat读入一个整数
outfile<<a<<" "; //将该数存放到f2.dat中
}
infile.close();
outfile.close();
}
//从f2.dat中读入20个整数,将它们按从小到大的顺序存放到f2.dat
void fun3()
{ifstream infile("f2.dat"); //定义输入文件流infile,以输入方式打开f2.dat
if(!infile)
{cerr<<"open f2.dat error!"<<endl;
exit(1);
}
inta[20];
inti,j,t;
for(i=0;i<20;i++)
infile>>a[i]; //从磁盘文件f2.dat读入20个数放在数组a中
for(i=0;i<19;i++) //用起泡法对20个数排序
for(j=0;j<19-i;j++)
if(a[j]>a[j+1])
{t=a[j];a[j]=a[j+1];a[j+1]=t;}
infile.close(); //关闭输入文件f2.dat
ofstream outfile("f2.dat",ios::out);
// f2.dat作为输出文件,文件中原有内容删除
if(!outfile)
{cerr<<"open f2.dat error!"<<endl;
exit(1);}
cout<<"data inf2.dat:"<<endl;
for( i=0;i<20;i++)
{outfile<<a[i]<<" "; //向f2.dat输出已排序的20个数
cout<<a[i]<<" ";} //同时输出到显示器
cout<<endl;
outfile.close();
}
int main()
{fun1(); //分别调用3个函数
fun2();
fun3();
return 0;
}