【负载均衡式在线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;
        }
    };
}
相关推荐
遇见火星3 小时前
Nginx 负载均衡配置模板:轮询、权重、IP哈希、最少连接
tcp/ip·nginx·负载均衡
budingxiaomoli1 天前
多机部署,负载均衡-LoadBalancer
运维·spring cloud·负载均衡
byoass1 天前
企业云盘高可用架构:主备切换、负载均衡与健康检查实战
运维·网络·安全·架构·云计算·负载均衡
Jinkxs2 天前
LoadBalancer- 主流负载均衡工具盘点:Nginx / Haproxy / Keepalived 基础介绍
运维·nginx·负载均衡
计算机安禾2 天前
【Linux从入门到精通】第48篇:Linux集群与负载均衡——LVS与Keepalived高可用
linux·负载均衡·lvs
云飞云共享云桌面3 天前
东莞智能装备工厂数字化实践—研发部门10名SolidWorks设计共享一台云主机流畅设计
服务器·自动化·汽车·负载均衡·制造
phltxy3 天前
微服务多机部署与负载均衡实战:从手写轮询到 Spring Cloud LoadBalancer 落地
spring cloud·微服务·负载均衡
日取其半万世不竭4 天前
用 Netdata 实时监控服务器,比 Prometheus + Grafana 轻量得多
linux·服务器·网络·系统架构·负载均衡·zabbix·grafana
信徒_4 天前
负载均衡技术选型
运维·负载均衡
CDN3604 天前
DNS 负载均衡技术架构与调度策略解析
运维·架构·负载均衡