【负载均衡式在线OJ】加载题目信息(文件版)

目录

[如何读取文件 -- 常见流程](#如何读取文件 -- 常见流程)

代码


如何读取文件 -- 常见流程

  • 在C++中使用 std::ifstream来打开文件流是一个常见的操作,用于创建一个输入文件流,并尝试打开名为 question_list的文件。
  • if (!in.is_open())检查文件是否成功打开。如果文件未能打开,通常是因为路径错误或权限问题。
  • while (getline(in, line)):使用 getline函数逐行读取文件直到结束。每次读取一行并存储在line字符串中。
  • in.close();显式地关闭文件流。尽管在in对象离开其作用域时会自动调用close方法,但在某些情况下显式关闭可能更好,特别是当你想要立即释放资源或者在同一作用域内重复使用同一个流对象时。
cpp 复制代码
#include <iostream>
#include <fstream>
#include <string>

int main() {
    // 文件名
    const char* filename = "question_list.txt";

    // 创建输入文件流对象
    std::ifstream in(filename);

    // 检查文件是否成功打开
    if (!in.is_open()) 
    {
        std::cerr << "无法打开文件: " << filename << std::endl;
        return 1; // 返回非零值表示程序异常终止
    }

    // 读取文件内容到字符串变量
    std::string line;
    while (getline(in, line)) // 使用getline逐行读取
    { 
        //读取该行内容,可以对根据内容处理数据
    }

    // 关闭文件流(当离开作用域时会自动调用close)
    in.close();

    return 0;
}

代码

OnlineJudge/oj_server/oj_model_file.hpp · zihuixie/负载均衡式在线OJ - 码云 - 开源中国https://gitee.com/zihuixie/load-balancing-online-oj/blob/master/OnlineJudge/oj_server/oj_model_file.hpp

cpp 复制代码
#pragma once
#include <unordered_map>
#include <string>
#include <vector>
#include <fstream>
#include <cassert>

#include "../comm/log.hpp"
#include "../comm/util.hpp"

// 文件版本,从文件中读取题目信息

namespace ns_model
{
    using namespace ns_log;
    using namespace ns_util;

    // 1 判断回文数 1 1 1000
    struct Question
    {
        std::string number; // 题号
        std::string title;  // 题目
        std::string star;   // 难度
        int cpu_limit;      // 时间限制
        int mem_limit;      // 空间限制

        std::string desc;   // 题目描述
        std::string header; // 提前预设的代码(用户未提交)
        std::string tail;   // 测试用例
    };

    const std::string question_path = "./questions/";               // 题库所在文件夹
    const std::string question_list = "./questions/questions/list"; // 题库清单

    class Model
    {
    private:
        // 题号->题目信息 的映射关系
        std::unordered_map<std::string, Question> questions;

    public:
        Model()
        {
            assert(LoadAllQuestions(question_list));
        }
        ~Model()
        {
        }

        // 从清单中加载题目信息到哈希表中
        bool LoadAllQuestions(const std::string &question_list)
        {
            std::ifstream in(question_list); // 打开流

            if (!in.is_open()) // 打开失败
            {
                LOG(FATAL) << " 加载题目列表失败,请检查是否存在题库文件 " << "\n";
                return false;
            }

            // 打开成功,开始读文件
            std::string line;
            std::vector<std::string> token;
            while (getline(in, line))
            {
                // 切割读到的字符串,并把字段插入到哈希表中

                // 1. 切割 line,把切割后的字段放入数组 token 中
                StringUtil::SplitString(line, &token, " ");

                // 2.把字段放入哈希表中
                //  1 判断回文数 1 1 1000

                if (token.size() != 5)
                {
                    LOG(WARNING) << " 部分题目格式错误,加载失败,请检查文件格式 " << "\n";
                    continue;
                }
                Question q;
                q.number = token[0];
                q.title = token[1];
                q.star = token[2];
                q.cpu_limit = std::stoi(token[3]);
                q.mem_limit = std::stoi(token[4]);

                // ./questions/1/
                std::string path = question_path;
                path += q.number;
                path += "/";

                FileUtil::ReadFile(path + "desc.txt", &(q.desc), true);
                FileUtil::ReadFile(path + "header.hpp", &(q.header), true);
                FileUtil::ReadFile(path + "tail.hpp", &(q.tail), true);

                questions.insert({q.number, q});
            }
            LOG(INFO)<<" 加载题库成功 "<<"\n";
            in.close();
        }

        // 获取整个题库
        bool GetAllQuestions(std::vector<Question> *out)
        {
            if (questions.empty())
            {
                LOG(ERROR) << " 用户获取题库失败 " << "\n";
                return false;
            }

            for (const auto &q : questions)
            {
                out->push_back(q.second);
            }
            return true;
        }

        // 获取指定题目
        bool GetOneQuestion(const std::string &number, Question *out)
        {
            if (questions.find(number) == questions.end())
            {
                LOG(ERROR) << "题目获取失败,题目编号:" << number << "\n";
                return false;
            }
            *out = questions[number];
            return true;
        }
    };
}
相关推荐
CodeDevMaster3 小时前
Gemini Balance:轻松实现Gemini API负载均衡与无缝切换的终极指南
负载均衡·api·gemini
扶风呀3 天前
具有熔断能力和活性探测的服务负载均衡解决方案
运维·负载均衡
Hello World呀3 天前
springcloud负载均衡测试类
spring·spring cloud·负载均衡
菜菜子爱学习4 天前
Nginx学习笔记(七)——Nginx负载均衡
笔记·学习·nginx·负载均衡·运维开发
扶风呀4 天前
负载均衡详解
运维·后端·微服务·面试·负载均衡
PXM的算法星球4 天前
spring gateway配合nacos实现负载均衡
spring·gateway·负载均衡
抛物线.7 天前
Docker Compose 部署高可用 MongoDB 副本集集群(含 Keepalived + HAProxy 负载均衡)
mongodb·docker·负载均衡
ZNineSun8 天前
什么是负载均衡,有哪些常见算法?
负载均衡·osi·七层网络模型
竹竿袅袅8 天前
Nginx 反向代理与负载均衡架构
nginx·架构·负载均衡
运维行者_9 天前
多数据中心运维:别让 “分布式” 变成 “混乱式”
运维·数据库·分布式·测试工具·自动化·负载均衡·故障告警