前言:
之前因为时间临时有事出门没有写出最后一题。
复习到文件读取那一章的时候才想起来这题忘记写,现在补一下
而且对于文件的读写是我的弱项,算是对知识点的巩固
题目:
写一个Student的结构体,包括学生的姓名,学号,语数英三科成绩
再写三个函数,一个是读操作,一个是写操作,一个是备份操作。
读写在Student.dat文件中,备份至Student.bak文件中
分析:
知识点:
头文件:#include<fstream>
读写操作:
写:ofstream write(const char *__s, streamsize __n)
例子:ofstream write((char*)&标识符,sizeof(数据类型))
读:ifstream read(const char *__s, streamsize __n)
例子:ofstream read((char*)&标识符,sizeof(数据类型))
文件打开模式:
ios::app | 追加模式,追加至文件尾 |
ios::binary | 以二进制的方式打开文件 |
ios::out | 输出(CPU视角),用来写文件 |
ios::in | 输入(CPU视角),用来读文件 |
我用的读写方式是二进制的方法读写,为了使程序更加清晰,我多加了一个函数是读取备份后的文件的( read_backup() ),用来检查是否备份成功。
在写的过程中,起初是在结构体中,对于姓名以及学号原本是采用string的数据类型,但是发现在读文件的时候会出现问题。
改成了字符数组的格式以后才成功。
源代码:
#include<iostream>
#include<fstream>
using namespace std;
//结构体
struct Student{
char name[20];
int id;
double Chinses;
double Math;
double English;
};
//写
void Write(Student a){
ofstream outfile("Student.dat",ios::binary|ios::app);
while(!outfile){
cout<<"error!"<<endl;
return;
}
outfile.write((char*)&a,sizeof(Student));
outfile.close();
}
//读
void read(){
ifstream infile("Student.dat",ios::binary);
while(!infile){
cout<<"error!"<<endl;
return;
}
Student a;
while(infile.read((char*)&a,sizeof(Student))){
cout<<endl<<"*******************"<<endl;
cout<<"name:"<<a.name<<endl;
cout<<"ID:"<<a.id<<endl;
cout<<"Chinese Score:"<<a.Chinses<<endl;
cout<<"Math score:"<<a.Math<<endl;
cout<<"English Score:"<<a.English<<endl;
}
infile.close();
}
//备份
void backup(){
ifstream infile("Student.dat",ios::binary);
while(!infile){
cout<<"error!"<<endl;
}
ofstream outfile("Student.bak",ios::app|ios::binary);
while(!outfile){
cout<<"error!"<<endl;
}
Student a;
while(infile.read((char*)&a,sizeof(Student))){
outfile.write((char*)&a,sizeof(Student));
}
infile.close();
outfile.close();
cout<<endl<<"successful!"<<endl;
}
//读取备份文件
void read_backup(){
ifstream infile("Student.bak",ios::binary);
while(!infile){
cout<<"error!"<<endl;
return;
}
Student a;
while(infile.read((char*)&a,sizeof(Student))){
cout<<endl<<"*******************"<<endl;
cout<<"name:"<<a.name<<endl;
cout<<"ID:"<<a.id<<endl;
cout<<"Chinese Score:"<<a.Chinses<<endl;
cout<<"Math score:"<<a.Math<<endl;
cout<<"English Score:"<<a.English<<endl;
}
infile.close();
}
int main()
{
cout<<"how many student you want to write:";
int n;
cin>>n;
Student *stu=new Student[n];
for(int i=0;i<n;i++){
cout<<endl;
cout<<"please input name:";
cin>>stu[i].name;
cout<<"please input ID:";
cin>>stu[i].id;
cout<<"please input Chinese Score:";
cin>>stu[i].Chinses;
cout<<"please input Math score:";
cin>>stu[i].Math;
cout<<"please input English Score:";
cin>>stu[i].English;
Write(stu[i]);
}
cout<<endl<<endl;
cout<<"Read:"<<endl;
read();
backup();
cout<<"Read backup:"<<endl;
read_backup();
return 0;
}
结果:
how many student you want to write:3
please input name:a
please input ID:101
please input Chinese Score:99
please input Math score:99
please input English Score:99
please input name:b
please input ID:102
please input Chinese Score:97
please input Math score:97
please input English Score:97
please input name:c
please input ID:103
please input Chinese Score:98
please input Math score:98
please input English Score:67
Read:
*******************
name:a
ID:101
Chinese Score:99
Math score:99
English Score:99
*******************
name:b
ID:102
Chinese Score:97
Math score:97
English Score:97
*******************
name:c
ID:103
Chinese Score:98
Math score:98
English Score:67
successful!
Read backup:
*******************
name:a
ID:101
Chinese Score:99
Math score:99
English Score:99
*******************
name:b
ID:102
Chinese Score:97
Math score:97
English Score:97
*******************
name:c
ID:103
Chinese Score:98
Math score:98
English Score:67