-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileIO.cpp
More file actions
90 lines (80 loc) · 2.7 KB
/
Copy pathFileIO.cpp
File metadata and controls
90 lines (80 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//
// Created by Wen34 on 2026/4/4.
//
#include "FileIO.h"
#include "struct.h"
#include <iostream>
#include <fstream>
#include <string>
void SaveData(const std::vector<Tmarks>& applicants, const std::string& filename)
{
std::ofstream outFile(filename);
if (!outFile.is_open())
{
std::cerr << "无法打开文件进行写入: " << filename << std::endl;
return;
}
outFile << applicants.size() << std::endl;
for (const auto& applicant : applicants)
{
outFile << applicant.name << " "
<< applicant.info.sex << " "
<< applicant.info.age << " "
<< applicant.info.schoolrecord << " "
<< applicant.info.worklen << " "
<< applicant.info.wordsite << " "
<< applicant.mark.pol << " "
<< applicant.mark.chn << " "
<< applicant.mark.eng << " "
<< applicant.mark.com << " "
<< applicant.mark.oral << " "
<< applicant.Srecord << " "
<< applicant.Sage << " "
<< applicant.Swlen << " "
<< applicant.total << std::endl;
}
outFile.close();
std::cout << "数据已成功保存到 " << filename << std::endl;
}
void LoadData(std::vector<Tmarks>& applicants, const std::string& filename)
{
std::ifstream inFile(filename);
if (!inFile.is_open())
{
std::cout << "未找到数据文件,将创建一个新列表。" << std::endl;
return;
}
int count;
inFile >> count;
applicants.clear();
applicants.reserve(count);
Tmarks temp;
for (int i = 0; i < count; ++i)
{
if (inFile >> temp.name
>> temp.info.sex
>> temp.info.age
>> temp.info.schoolrecord
>> temp.info.worklen
>> temp.info.wordsite
>> temp.mark.pol
>> temp.mark.chn
>> temp.mark.eng
>> temp.mark.com
>> temp.mark.oral
>> temp.Srecord
>> temp.Sage
>> temp.Swlen
>> temp.total)
{
applicants.push_back(temp);
}
else
{
std::cerr << "读取第 " << i + 1 << " 条数据时出错,文件可能损坏。" << std::endl;
break;
}
}
inFile.close();
std::cout << "成功从 " << filename << " 加载了 " << applicants.size() << " 条记录。" << std::endl;
}