1 Star 0 Fork 0

开放轨迹与人工智能/CPlusPlusThings

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
read_file.cpp 1.71 KB
一键复制 编辑 原始数据 按行查看 历史
/* 读写文件综合题.cpp */
#include <cstring>
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
class Person {
public:
Person() {}
Person(char *name, char *id, int math, int chinese, int english) {
strcpy(Name, name);
strcpy(Id, id);
Math = math;
Chinese = chinese;
English = english;
Sum = Math + Chinese + English;
}
void display() {
cout << Name << "\t" << Id << "\t" << Math << "\t" << Chinese << "\t"
<< English << "\t" << Sum << endl;
}
private:
char Name[20];
char Id[20];
int Math;
int Chinese;
int English;
int Sum;
};
int main(int argc, char const *argv[]) {
char ch;
char Name[20], Id[20];
int Math, Chinese, English;
fstream ioFile;
ioFile.open("d:/per.dat", ios::out | ios::app);
cout << "---------建立学生档案信息----------\n";
do {
cout << "请输入姓名:";
cin >> Name;
cout << "请输入身份证号:";
cin >> Id;
cout << "请输入数学成绩:";
cin >> Math;
cout << "请输入汉语成绩:";
cin >> Chinese;
cout << "请输入英语成绩:";
cin >> English;
Person per(Name, Id, Math, Chinese, English);
ioFile.write((char *)&per, sizeof(per));
cout << "您是否继续建档?(Y/y) ";
cin >> ch;
} while (ch == 'y' || ch == 'Y');
ioFile.close();
ioFile.open("d://per.dat", ios::in);
Person p;
ioFile.read((char *)&p, sizeof(p));
vector<Person> v;
vector<Person>::iterator vt;
while (!ioFile.eof()) {
v.push_back(p);
ioFile.read((char *)&p, sizeof(p));
}
ioFile.close();
cout << "---------输出档案信息-----------" << endl;
for (vt = v.begin(); vt != v.end(); vt++) {
(*vt).display();
}
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/Open_FootSteps/CPlusPlusThings.git
git@gitee.com:Open_FootSteps/CPlusPlusThings.git
Open_FootSteps
CPlusPlusThings
CPlusPlusThings
master

搜索帮助

D67c1975 1850385 1daf7b77 1850385