实验六-文件I/O与异常处理

实验任务1源代码:

 1 #pragma once
 2 #include <iomanip>
 3 #include <iostream>
 4 #include <string>
 5 
 6 struct Contestant {
 7     long   id;              // 学号
 8     std::string name;       // 姓名
 9     std::string major;      // 专业
10     int    solved;          // 解题数
11     int    penalty;         // 总罚时
12 };
13 
14 // 重载<<
15 // 要求:姓名/专业里不含空白符
16 inline std::ostream& operator<<(std::ostream& out, const Contestant& c) {
17     out << std::left;
18     out << std::setw(15) << c.id
19         << std::setw(15) << c.name
20         << std::setw(15) << c.major
21         << std::setw(10) << c.solved
22         << std::setw(10) << c.penalty;
23 
24     return out;
25 }
26 
27 // 重载>>
28 inline std::istream& operator>>(std::istream& in, Contestant& c) {
29     in >> c.id >> c.name >> c.major >> c.solved >> c.penalty;
30 
31     return in;
32 }
contestant.hpp
 1 #pragma once
 2 #include <fstream>
 3 #include <iostream>
 4 #include <stdexcept>
 5 #include <string>
 6 #include <vector>
 7 #include "contestant.hpp"
 8 
 9 // ACM 排序规则:先按解题数降序,再按罚时升序
10 inline bool cmp_by_solve(const Contestant& a, const Contestant& b) {
11     if (a.solved != b.solved)
12         return a.solved > b.solved;
13 
14     return a.penalty < b.penalty;
15 }
16 
17 // 将结果写至任意输出流
18 inline void write(std::ostream& os, const std::vector<Contestant>& v) {
19     for (const auto& x : v)
20         os << x << '\n';
21 }
22 
23 // 将结果打印到屏幕
24 inline void print(const std::vector<Contestant>& v) {
25     write(std::cout, v);
26 }
27 
28 // 将结果保存到文件
29 inline void save(const std::string& filename, const std::vector<Contestant>& v) {
30     std::ofstream os(filename);
31     if (!os)
32         throw std::runtime_error("fail to open " + filename);
33 
34     write(os, v);
35 }
36 
37 // 从文件读取信息(跳过标题行)
38 inline std::vector<Contestant> load(const std::string& filename) {
39     std::ifstream is(filename);
40     if (!is)
41         throw std::runtime_error("fail to open " + filename);
42 
43     std::string line;
44     std::getline(is, line);          // 跳过标题
45 
46     std::vector<Contestant> v;
47     Contestant t;
48     int seq;
49     while (is >> seq >> t)
50         v.push_back(t);
51 
52     return v;
53 }
utils.hpp
 1 #include <algorithm>
 2 #include <iostream>
 3 #include <stdexcept>
 4 #include <vector>
 5 #include "contestant.hpp"
 6 #include "utils.hpp"
 7 
 8 const std::string in_file = "./data.txt";
 9 const std::string out_file = "./ans.txt";
10 
11 void app() {
12     std::vector<Contestant> contestants;
13 
14     try {
15         contestants = load(in_file);
16         std::sort(contestants.begin(), contestants.end(), cmp_by_solve);
17         print(contestants);
18         save(out_file, contestants);
19     }
20     catch (const std::exception& e) {
21         std::cerr << e.what() << '\n';
22         return;
23     }
24 }
25 
26 int main() {
27     app();
28 }
task1.cpp

实验任务1运行结果:

QQ_1766395062477

  实验任务1问题1:

  (1)C++标准库中的流类之间存在继承关系

  (2)不需要

  实验任务1问题2:

  (1)save函数中:打开输出文件失败时会抛出异常,load函数中:打开输入文件失败时会抛出异常

  (2)被app函数中try-catch捕获

  实验任务1问题3:可以,效果完全一致

  实验任务1问题4:

  (1)运行结果:

QQ_1766395962087

  原因:程序运行可能会出现数据读取错误,读取不完整甚至中断

实验任务2源代码:

 1 #include <iostream>
 2 #include <limits>
 3 #include <string>
 4 #include "stumgr.hpp"
 5 
 6 const std::string in_file = "./data.txt";
 7 const std::string out_file = "./ans.txt";
 8 
 9 void menu() {
10     std::cout << "\n**********简易应用**********\n"
11         "1. 加载文件\n"
12         "2. 排序\n"
13         "3. 打印到屏幕\n"
14         "4. 保存到文件\n"
15         "5. 退出\n"
16         "请选择:";
17 }
18 
19 void app() {
20     StuMgr mgr;
21 
22     while (true) {
23         menu();
24         int choice;
25         std::cin >> choice;
26 
27         try {
28             switch (choice) {
29             case 1: mgr.load(in_file);
30                 std::cout << "加载成功\n"; break;
31             case 2: mgr.sort();
32                 std::cout << "排序已完成\n"; break;
33             case 3: mgr.print();
34                 std::cout << "打印已完成\n"; break;
35             case 4: mgr.save(out_file);
36                 std::cout << "导出成功\n"; break;
37             case 5: return;
38             default: std::cout << "不合法输入\n";
39             }
40         }
41         catch (const std::exception& e) {
42             std::cout << "Error: " << e.what() << '\n';
43         }
44     }
45 }
46 
47 int main() {
48     app();
49 }
task2.cpp
 1 #pragma once
 2 
 3 #include <iostream>
 4 #include <string>
 5 
 6 class Student {
 7 public:
 8     Student() = default;
 9     ~Student() = default;
10 
11     const std::string get_major() const;
12     int get_grade() const;
13 
14     friend std::ostream& operator<<(std::ostream& os, const Student& s);
15     friend std::istream& operator>>(std::istream& is, Student& s);
16 
17 private:
18     int id;
19     std::string  name;
20     std::string  major;
21     int          grade;  // 0-100
22 };
student.hpp
 1 #pragma once
 2 #include <string>
 3 #include <vector>
 4 #include "student.hpp"
 5 
 6 class StuMgr {
 7 public:
 8     void load(const std::string& file);  // 加载数据文件(空格分隔)
 9     void sort();                         // 排序: 按专业字典序升序、同专业分数降序
10     void print() const;                  // 打印到屏幕
11     void save(const std::string& file) const; // 保存到文件
12 
13 private:
14     void write(std::ostream& os) const;  // 把数据写到任意输出流
15 
16 private:
17     std::vector<Student> students;
18 };
stumgr.hpp
 1 #include "student.hpp"
 2 #include <iostream>
 3 #include <stdexcept>
 4 
 5 const std::string Student::get_major() const {
 6     return major;
 7 }
 8 
 9 int Student::get_grade() const {
10     return grade;
11 }
12 
13 std::ostream& operator<<(std::ostream& os, const Student& s) {
14     os << s.id << " " << s.name << " " << s.major << " " << s.grade;
15     return os;
16 }
17 
18 std::istream& operator>>(std::istream& is, Student& s) {
19     is >> s.id >> s.name >> s.major >> s.grade;
20 
21     return is;
22 }
student.cpp
 1 #include "stumgr.hpp"
 2 #include <fstream>
 3 #include <algorithm>
 4 #include <stdexcept>
 5 
 6 void StuMgr::load(const std::string& file) {
 7     std::ifstream in_file(file);
 8     if (!in_file.is_open()) {
 9         throw std::runtime_error("cannot open file:"+ file);
10     }
11 
12     students.clear();
13     Student s;
14 
15     try {
16         while (in_file >> s) {
17             students.push_back(s);
18         }
19     }
20     catch(const std::exception& e) {
21         in_file.close();
22         throw std::runtime_error("file is wrong:" + std::string(e.what()));
23     }
24 
25     in_file.close();
26 
27     if (students.empty()) {
28         throw std::runtime_error("file is null or data is wrong");
29     }
30 }
31 
32 void StuMgr::sort() {
33     std::sort(students.begin(), students.end(),
34         [](const Student& a, const Student& b) {
35         if (a.get_major() != b.get_major()) {
36             return a.get_major() < b.get_major();
37         }
38         return a.get_grade() > b.get_grade();
39         });
40 }
41 
42 void StuMgr::print() const {
43     if (students.empty()) {
44         std::cout << "暂无学生数据" << std::endl;
45         return;
46     }
47 
48     std::cout << "学号\t姓名\t专业\t成绩\t" << std::endl;
49     
50     write(std::cout);
51 }
52 
53 void StuMgr::save(const std::string& file) const {
54     std::ofstream out_file(file);
55     if (!out_file.is_open()) {
56         throw std::runtime_error("cannot creat file:" + file);
57     }
58 
59     try {
60         write(out_file);
61     }
62     catch(const std::exception& e) {
63         out_file.close();
64         throw;
65     }
66 
67     out_file.close();
68 }
69 
70 void StuMgr::write(std::ostream& os) const {
71     for (const auto& student : students) {
72         os << student << std::endl;
73     }
74 }
stumgr.cpp

实验任务2运行结果:

QQ_1766406792525

QQ_1766406808604

QQ_1766406844497

 

posted on 2025-12-22 20:35  豪雅  阅读(0)  评论(0)    收藏  举报