【负载均衡式在线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;
        }
    };
}
相关推荐
章老师说1 天前
NGINX官方谈Lua风险:这其实是两条网关技术路线之争
运维·nginx·负载均衡·lua·openresty
LiaoWL1231 天前
【SpringCloud合集-02】Spring Cloud LoadBalancer 负载均衡器学习笔记
学习·spring cloud·负载均衡
姚不倒2 天前
F5 SSL Profile 证书卸载深入篇
运维·网络协议·负载均衡·ssl·f5
oscar9992 天前
3.4 Nginx 负载均衡——动态再平衡的反人性纪律
nginx·github·负载均衡·财富源代码
姚不倒2 天前
F5 健康检查 Monitor 深入篇
运维·负载均衡·f5
我是一颗柠檬4 天前
【Java项目技术亮点】加权轮询负载均衡算法
java·算法·负载均衡
fei_sun5 天前
等价负载均衡(等价路由ECMP)
运维·负载均衡
我登哥MVP23 天前
SpringCloud Alibaba 核心组件解析:服务调用和负载均衡
java·spring boot·后端·spring·spring cloud·java-ee·负载均衡
Boop_wu24 天前
[Spring Cloud] Nacos核心功能详解:从负载均衡到配置中心的全链路实践
spring·spring cloud·负载均衡
isyangli_blog24 天前
基于 OpenDaylight 的 SDN 负载均衡应用
运维·负载均衡