【负载均衡式在线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;
        }
    };
}
相关推荐
码上飞扬20 小时前
Nginx负载均衡配置详解:轻松实现高可用与高性能
运维·nginx·负载均衡
程序猿000001号21 小时前
Java实现Consul/Nacos根据GPU型号、显存余量执行负载均衡
nacos·负载均衡
清河九月2 天前
Nginx 正向代理、反向代理和负载均衡
运维·nginx·负载均衡
01_3 天前
一致性哈希函数处理负载均衡(简单实现,勿喷)
算法·负载均衡·哈希算法
天上掉下来个程小白3 天前
开发环境搭建-06.后端环境搭建-前后端联调-Nginx反向代理和负载均衡概念
java·运维·spring boot·后端·nginx·负载均衡·苍穹外卖
obboda3 天前
web高可用集群项目(数据库主从同步、文件共享存储、nginx动静分离+负载均衡+高可用)
数据库·nginx·负载均衡
小技工丨5 天前
kakfa-3:ISR机制、HW&LEO、生产者、消费者、核心参数&负载均衡
大数据·运维·kafka·负载均衡
GGGGGGGGGGGGGG.5 天前
nginx+keepalived负载均衡及高可用
前端·nginx·负载均衡
weixin_425878236 天前
Nginx负载均衡策略详解:从轮询到智能分发,打造高可用服务架构
nginx·架构·负载均衡
_.Switch7 天前
高效API开发:FastAPI中的缓存技术与性能优化
python·缓存·性能优化·负载均衡·fastapi