标签:com class c++学习 .com [] 图片 strcpy file aaa
1 //二进制文件的读和写 2 //istream & istream::read(char *,int); 3 //ostream & ostream::write(const char *,int); 4 #include<fstream.h> 5 #include<string.h> 6 7 class Student{ 8 public: 9 char name[20]; 10 long number; 11 int score; 12 Student(){} 13 Student(char *na,long nu=0,int sc=0){ 14 strcpy(name,na); 15 number=nu; 16 score=sc; 17 } 18 }; 19 20 int main(){ 21 char fileName[]="aaa.dat"; 22 Student stu1("张三",111,100),stu2; 23 ofstream fileOut(fileName,ios::binary); 24 fileOut.write((char *)&stu1,sizeof(class Student)); 25 fileOut.close(); 26 27 ifstream fileIn(fileName,ios::binary); 28 fileIn.read((char *)&stu2,sizeof(class Student)); 29 fileIn.close(); 30 31 cout<<"姓名:"<<stu2.name<<endl; 32 cout<<"学号:"<<stu2.number<<endl; 33 cout<<"成绩:"<<stu2.score<<endl; 34 return 0; 35 }
aaa.dat文件的内容是
1 张三 烫烫烫烫烫烫烫蘯 d
标签:com class c++学习 .com [] 图片 strcpy file aaa
原文地址:https://www.cnblogs.com/Tobi/p/9251655.html