Skip to content

Commit ecb8b3c

Browse files
committed
🐛 Mend the bug: can't write in and read the file
1 parent df4155e commit ecb8b3c

File tree

11 files changed

+58
-13
lines changed

11 files changed

+58
-13
lines changed

Functions.cpp

+29-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ void Exit() {
1717
//read用于将文件中的信息重新储存到容器中
1818
void ReadInfo(std::vector<std::unique_ptr<Person>> &loadedObjects) {
1919
// 从文件加载对象
20-
std::ifstream ifs("WokerManager.txt", std::ios::binary);
20+
std::ifstream ifs("WorkerManager.txt", std::ios::binary);
2121
if (!ifs.is_open()) {
2222
throw std::runtime_error("Could not open file for reading.");
2323
}
@@ -26,24 +26,44 @@ void ReadInfo(std::vector<std::unique_ptr<Person>> &loadedObjects) {
2626
try {
2727
auto obj = Person::deserialize(ifs);
2828
loadedObjects.push_back(std::move(obj));
29-
} catch (const std::exception&) {
30-
break; // 如果文件结束,捕获异常并退出循环
29+
} catch (const std::exception& e) {
30+
// 捕获异常
31+
if (ifs.eof()) { // 检查是否到达文件末尾
32+
break;
33+
}
34+
std::cerr << "Error during deserialization: " << e.what() << std::endl;
35+
throw; // 重新抛出异常以通知调用者
3136
}
3237
}
3338
ifs.close();
3439
}
3540

3641
void SaveInfo(const std::vector<std::unique_ptr<Person>> &objects) {
3742
// 保存到文件
38-
std::ofstream ofs("WorkerManager.txt", std::ios::binary);
43+
std::ofstream ofs("WorkerManager.txt", std::ios::binary | std::ios::out);
3944
if (!ofs.is_open()) {
4045
throw std::runtime_error("Could not open file for writing.");
4146
}
4247

43-
for (const auto& obj : objects) {
44-
obj->write(ofs);
48+
try {
49+
for (const auto& obj : objects) {
50+
if (!obj) {
51+
std::cerr << "Encountered null pointer in object list." << std::endl;
52+
continue; // 或者抛出异常,取决于具体需求
53+
}
54+
obj->write(ofs);
55+
// 检查输出是否成功
56+
if (!ofs) {
57+
std::cerr << "Error writing to file." << std::endl;
58+
break; // 停止写入,避免更多的错误
59+
}
60+
}
61+
} catch (const std::exception&e) {
62+
std::cerr << "Error"<<e.what()<<std::endl;
63+
// 在捕获所有异常的情况下关闭文件
64+
// ofs.close(); // 这里实际上不需要调用 close,因为 ofs 已经在作用域结束时自动关闭
65+
throw; // 重新抛出异常
4566
}
46-
ofs.close();
4767
}
4868

4969

@@ -183,7 +203,7 @@ void ModifyInformation(std::vector<std::unique_ptr<Person>> &Objects) {
183203
bool found=false;
184204
for(const auto& obj : Objects) {
185205
if(obj->Number()==num) {
186-
std::cout << "Please input the new name: ";
206+
std::cout << "Please input the new number: ";
187207
int newNum;
188208
std::cin >> newNum;
189209
obj->ChangeNumber(newNum);
@@ -313,6 +333,7 @@ void SortInformation(std::vector<std::unique_ptr<Person>> &Objects) {
313333

314334
void ClearAlltxt(std::vector<std::unique_ptr<Person>> &Objects) {
315335
Objects.clear();
336+
SaveInfo(Objects);
316337
std::cout << "All information has been cleared." << std::endl;
317338
}
318339

WorkerClass.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
std::unique_ptr<Person> Person::deserialize(std::istream& is) {
99
int kind;
1010
is >> kind;
11-
12-
1311
switch (kind) {
1412
case Person::Worker:
1513
return Worker::fromStream(is);

WorkerClass.h

+12
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ class Worker : public Person {
6666
void ShowDuty() override ;
6767
void write(std::ostream& os) const override {
6868
os << static_cast<int>(Person::Worker) << ' ' << Number() << ' ' << Name() << '\n';
69+
// 检查输出是否成功
70+
if (!os) {
71+
std::cerr << "Error writing to stream." << std::endl;
72+
}
6973
}
7074

7175
void read(std::istream& is) override ;
@@ -103,6 +107,10 @@ class Manager : public Person {
103107

104108
void write(std::ostream& os) const override {
105109
os << static_cast<int>(Person::Manager) << ' ' << Number() << ' ' << Name() << '\n';
110+
// 检查输出是否成功
111+
if (!os) {
112+
std::cerr << "Error writing to stream." << std::endl;
113+
}
106114
}
107115

108116
void read(std::istream& is) override ;
@@ -140,6 +148,10 @@ class Boss : public Person {
140148

141149
void write(std::ostream& os) const override {
142150
os << static_cast<int>(Person::Boss) << ' ' << Number() << ' ' << Name() << '\n';
151+
// 检查输出是否成功
152+
if (!os) {
153+
std::cerr << "Error writing to stream." << std::endl;
154+
}
143155
}
144156

145157
void read(std::istream& is) override ;

cmake-build-debug/.ninja_deps

4.99 KB
Binary file not shown.

cmake-build-debug/.ninja_log

+10
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,13 @@
7171
4 396 7450902753258483 CMakeFiles/WorkerManagerSystem.dir/WorkerClass.cpp.obj 80e12e65f852bef9
7272
16 476 7450902753383197 CMakeFiles/WorkerManagerSystem.dir/Functions.cpp.obj 3d61f7412b6d32b0
7373
476 606 7450902757973532 WorkerManagerSystem.exe 4ce8cc964464a38d
74+
21 282 7451681599762598 CMakeFiles/WorkerManagerSystem.dir/context.cpp.obj ab7e873d7c93df0c
75+
7 341 7451681599631404 CMakeFiles/WorkerManagerSystem.dir/main.cpp.obj 3ca4f3006d423f01
76+
33 524 7451681599861479 CMakeFiles/WorkerManagerSystem.dir/Functions.cpp.obj 3d61f7412b6d32b0
77+
524 657 7451681604763493 WorkerManagerSystem.exe 4ce8cc964464a38d
78+
6 368 7452217620757089 CMakeFiles/WorkerManagerSystem.dir/main.cpp.obj 3ca4f3006d423f01
79+
18 394 7452217620880097 CMakeFiles/WorkerManagerSystem.dir/WorkerClass.cpp.obj 80e12e65f852bef9
80+
30 531 7452217621000298 CMakeFiles/WorkerManagerSystem.dir/Functions.cpp.obj 3d61f7412b6d32b0
81+
531 668 7452217626011925 WorkerManagerSystem.exe 4ce8cc964464a38d
82+
3 1590 7452222951452804 CMakeFiles/WorkerManagerSystem.dir/Functions.cpp.obj 3d61f7412b6d32b0
83+
1590 7183 7452222967315546 WorkerManagerSystem.exe 4ce8cc964464a38d
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
Start testing: Aug 12 19:46 �й���׼ʱ��
1+
Start testing: Aug 13 11:18 �й���׼ʱ��
22
----------------------------------------------------------
3-
End testing: Aug 12 19:46 �й���׼ʱ��
3+
End testing: Aug 13 11:18 �й���׼ʱ��
124 KB
Binary file not shown.

cmake-build-debug/Workermanager.txt

+5-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
3 1001 Aiden
1+
3 3001 Already
2+
3 3002 Jerry
3+
3 3003 Aodaly
4+
2 2001 Cihelo
5+
2 2006 ASAP

0 commit comments

Comments
 (0)