7.5

#include  
#include  
using namespace std; 
typedef struct staff 
{
 int num; 
 char name[20]; 
 int age; 
 double  pay; 
}; 
int main() 
{
 staff staf[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; 
 fstream iofile("stu.dat",ios::in|ios::out|ios::binary); 
 if(!iofile) 
  {
   cerr<<"open error!"<>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<>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; 
 } 
  iofile.close(); 
  return 0; 
} 

你可能感兴趣的:(第七章输入输出流)