题目:
编写一个程序,实现以下功能:
(1)输入一系列的学生数据(包括学生的学号、姓名和成绩等基本信息),将学生信息写入二进制文件student.dat中。
(2)从student.dat文件中读出这些数据并显示出来。
(3)在student.dat文件中按姓名进行查询,如输入“李”,则将所有姓“李”的学生的数据显示出来。
(4)可对指定学生的数据进行修改。
(5)可以删除指定的学生数据。
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<string.h>
using namespace std;
void addFile(); //增加数据
void showFile(); //显示数据
void findFile(); //查询功能
void editFile(); //修改文件
void deleteFile();//删除指定数据
class Student
{
public:
char name[20];
float score;
int sno;
};
int main()
{
int choice;
while(true)
{
cout<<"输入你选择的功能项目对应的数字:"<<endl;
cout<<"1.增加数据"<<endl;
cout<<"2.显示所有学生数据"<<endl;
cout<<"3.按姓名进行查询"<<endl;
cout<<"4.修改指定学生数据"<<endl;
cout<<"5.删除指定学生数据"<<endl;
cout<<"6.退出"<<endl;
cin>>choice;
if(choice>=1&&choice<=6)
{
switch(choice)
{
case 1:addFile();break;
case 2:showFile();break;
case 3:findFile();break;
case 4:editFile();break;
case 5:deleteFile();break;
case 6:exit(0);break;
}
}
else
{
cout<<"输入数字有误,请重新输入!"<<endl;
}
}
return 0;
}
void addFile()//添加函数
{
fstream file;
Student stu;
char again;
int i;
file.open("student.dat",ios::app|ios::out|ios::binary);//以追加形式打开二进制文件
if(!file)
{
cout<<"error";
exit(0);
}
do{
cout<<"请输入以下数据:"<<endl;
cout<<"姓名:";
cin>>stu.name;
cout<<"学号:";
cin>>stu.sno;
cin.ignore();
cout<<"成绩:";
cin>>stu.score;
cin.ignore();
file.write((char *)&stu,sizeof(stu));
cout<<"是否还要输入一个学生的数据(y or n):";
cin>>again;
cin.ignore();
}while(again=='y');//循环输入添加学生信息
file.close();
}
void showFile() //显示文件
{
fstream file;
file.open("student.dat",ios::binary|ios::in);//打开文件
if(!file)
{
cout<<"error";
exit(0);
}
Student stu;
while(!file.eof())//循环打印
{
file.read((char *)&stu,sizeof(stu));
if(file.fail())
break;
cout<<"姓名:"<<stu.name<<endl;
cout<<"学号:"<<stu.sno<<endl;
cout<<"成绩:"<<stu.score<<endl;
}
file.close();
}
void findFile() //查询功能
{
fstream file;
file.open("student.dat",ios::binary|ios::in);//打开文件
file.clear();
file.seekg(0L,ios::beg);
Student stu;
char t[10];
cout<<"请输入姓氏:";
cin>>t;
while(!file.eof())//循环判断前两个字符是否相等来查找打印学生信息
{
file.read((char *)&stu,sizeof(stu));
if(file.fail())
break;
file.seekg(0L,ios::cur);
if(t[0]==stu.name[0]&&t[1]==stu.name[1])
{
cout<<"查找成功!"<<endl;
cout<<"姓名:"<<stu.name<<endl;
cout<<"学号:"<<stu.sno<<endl;
cout<<"成绩:"<<stu.score<<endl;
}
}
file.close();
}
void editFile() //修改文件
{
fstream file,file1;
file.open("student.dat",ios::binary|ios::in);//打开文件和中间文件
file1.open("delete.dat",ios::binary|ios::out);
if(!file)
{
cout<<"error";
exit(0);
}
if(!file1)
{
cout<<"error";
exit(0);
}
int t;
Student stu;
cout<<"请输入要修改的学号:";
cin>>t;
while(!file.eof())//循环将原文件和修改的数据存入中间文件
{
file.read((char *)&stu,sizeof(stu));
if(file.fail())
break;
if(stu.sno==t)
{
cout<<"请输入要修改的姓名:";
cin>>stu.name;
cout<<"请输入要修改的学号:";
cin>>stu.sno;
cout<<"请输入要修改的成绩:";
cin>>stu.score;
}
file1.write((char *)&stu,sizeof(stu));
}
file.close();
file1.close();
file.open("student.dat",ios::out|ios::binary);
file1.open("delete.dat",ios::in|ios::binary);
while(!file1.eof())//将中间文件数据存入原文件
{
file1.read((char *)&stu,sizeof(stu));
if(file1.fail())
break;
file.write((char *)&stu,sizeof(stu));
}
cout<<"修改成功!"<<endl;
file.close();
file1.close();
}
void deleteFile() //删除指定数据
{
fstream file,file1;
file.open("student.dat",ios::binary | ios::in);//打开文件
file1.open("delete.dat",ios::binary | ios::out);//打开中间文件
if(!file)
{
cout<<"error";
exit(0);
}
if(!file1)
{
cout<<"error";
exit(0);
}
int t;
Student stu;
cout<<"请输入要删除的学号:";
cin>>t;
while(!file.eof())//循环将除了要删除的学生之外的学生信息存入中间文件
{
file.read((char *)&stu,sizeof(stu));
if(file.fail())
break;
if(stu.sno==t)
continue;
file1.write((char *)&stu,sizeof(stu));
}
file.close();
file1.close();
file.open("student.dat",ios::out|ios::binary);//改变打开方式
file1.open("delete.dat",ios::in|ios::binary);
while(!file1.eof())//将中间文件存入原文件
{
file1.read((char *)&stu,sizeof(stu));
if(file1.fail())
break;
file.write((char *)&stu,sizeof(stu));
}
file.close();
file1.close();
cout<<"删除成功!"<<endl;
}