功能描述
用文件系统实现学籍管理,学生的信息包括学号、姓名、性别、年龄、专业和奖励。
数据存储说明:
1.定长记录 存储在“学生基本信息”文件中
2.变长记录 存放在另一个“奖励”文件
3.“学生基本信息”表中的位置和长度描述“奖励”文件中记录的开始位置和长度
编写应用程序,实现数据的录入和查找
源代码
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class student { //学生类
public:
string id, name, major, reward, sex; //学号、姓名、专业、奖励、性别
int age, begin, end; //年龄、奖励数据开始位置、奖励数据结束位置
static int start; //记录下次录入数据时的奖励数据开始位置
public:
student(string id = "", string name = "", string major = "", string sex = "",
int age = 0, string reward = "") {
this->id = id;
this->name = name;
this->major = major;
this->sex = sex;
this->age = age;
this->reward = reward;
this->begin = 0;
this->end = 0;
}
static int initStart(); //用于初始化start变量
};
int student::initStart() { //初始化start变量,在main函数之前执行
struct stat st;
stat("rewardInfo.txt", &st);
return st.st_size; //返回rewardInfo.txt文件内容的长度
}
int student::start = student::initStart(); //为start赋值
void saveData(student s) { //保存数据至文件
ofstream fp;
//追加写方式,打开studentInfo.txt文件
fp.open("studentInfo.txt",ios::out | ios::app);
//将数据转换为字符串
string age = to_string(s.age), left = to_string(s.begin), right = to_string(s.end);
age.resize(5, ' '); left.resize(5, ' '); right.resize(5, ' '); //统一数据长度
string data = "";
data += s.id + s.name + s.major + age + s.sex + left + right; //连接字符串
fp << data << endl; //将数据写入文件
fp.close(); //关闭文件
fp.open("rewardInfo.txt", ios::out | ios::app); //打开rewardInfo.txt文件
fp << s.reward; //将奖励信息写入文件
fp.close(); //关闭文件
}
void getInput() { //读入数据,生成学生信息
student s;
cout << "请输入学生信息:学号、姓名、性别、年龄、专业、奖励" << endl;
cin >> s.id >> s.name >> s.sex >> s.age >> s.major >> s.reward;
s.id.resize(20,' '); //统一数据长度
s.name.resize(20, ' ');
s.sex.resize(5, ' ');
s.major.resize(30, ' ');
s.begin = student::start;
s.end = s.begin + s.reward.size(); //计算奖励结束位置
student::start += s.reward.size(); //更新start数值
saveData(s); //保存学生信息
cout << "数据添加成功!" << endl;
}
void searchInfo(string info) { //查找学生数据
string id = info;
id.resize(20, ' '); //填充字符串,用于比对
ifstream fp("studentInfo.txt", ios::in); //打开文件
string data;
bool flag = false; //flag为false表示未找到数据,true为已找到数据
while (!fp.eof()) {
getline(fp, data); //按行读数据
if (data.substr(0,20) == id) {
flag = true;
cout << endl << "学号为" << info << "的学生信息如下:" << endl;
cout << "姓名:" << data.substr(20, 20) << endl;
cout << "专业:" << data.substr(40, 30) << endl;
cout << "年龄:" << data.substr(70, 5) << endl;
cout << "性别: " << data.substr(75, 5) << endl;
int left = stoi(data.substr(80, 5));
int len = stoi(data.substr(85, 5)) - left; //计算奖励数据长度
ifstream rewardFile("rewardInfo.txt", ios::in); //打开文件
string reward;
rewardFile >> reward;
//截取字符串并输出
cout << "奖励:" << reward.substr(left, len) << endl;
cout << "----------查询成功----------" << endl;
rewardFile.close(); //关闭文件
break;
}
}
if (!flag) {
cout << "-----查询失败,没有当前学号对应的数据!-----" << endl;
}
fp.close();
}
int main() {
cout << "-------------------------学生信息管理-------------------------" << endl << endl;
string select;
while (true) {
cout << endl << "1--->录入信息 2--->查找信息 quit--->退出程序" << endl << "请选择:";
cin >> select;
if (select == "1") {
getInput();
}
else if (select == "2") {
string id;
cout << "请输入要查询的学生的学号:";
cin >> id;
searchInfo(id);
}
else if (select == "quit") {
//saveData(stus);
cout << "提示:学生基本数据已保存至studentInfo.txt,奖励数据保存至rewardInfo.txt" << endl << endl;
cout << "-----程序已正常退出!-----" << endl;
break; //退出
}
else {
cout << endl << "输入无效,请重新输入!" << endl;
}
}
return 0;
}