1 #include <iostream> 2 #include <fstream> 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 4 using namespace std; 5 struct student 6 { 7 string name; 8 int num; 9 int age; 10 char sex; 11 }; 12 int main(int argc, char** argv) { 13 student stud[3]; 14 int i; 15 ifstream infile("stud.dat",ios::binary); 16 if(!infile) 17 { 18 cerr<<"open error!"<<endl; 19 abort(); 20 } 21 for(i=0;i<3;i++) 22 infile.read((char*)&stud[i],sizeof(stud[i])); 23 infile.close(); 24 for(i=0;i<3;i++) 25 { 26 cout<<"NO."<<i+1<<endl; 27 cout<<"name:"<<stud[i].name<<endl; 28 cout<<"num:"<<stud[i].num<<endl; 29 cout<<"age:"<<stud[i].age<<endl; 30 cout<<"sex:"<<stud[i].sex<<endl; 31 } 32 return 0; 33 }