首页 > 编程语言 >C++读取配置文件

C++读取配置文件

时间:2024-05-17 11:10:34浏览次数:13  
标签:std 读取 配置文件 C++ file line include config string

1、读取=号的配置文件(或者:)的配置。

#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <string>

std::map<std::string, std::string> read_config(const std::string &filename) {
    std::map<std::string, std::string> config;
    std::ifstream file(filename);
    std::string line;

    if (!file.is_open()) {
        throw std::runtime_error("Unable to open config file");
    }

    while (getline(file, line)) {
        std::istringstream is_line(line);
        std::string key;
        if (getline(is_line, key, '=')) {
            std::string value;
            if (getline(is_line, value)) {
                config[key] = value;
            }
        }
    }

    file.close();
    return config;
}

int main() {
    try {
        auto config = read_config("config.ini");
        for (const auto &pair : config) {
            std::cout << pair.first << " = " << pair.second << std::endl;
        }
    }
    catch (const std::exception &e) {
        std::cerr << "Error: " << e.what() << std::endl;
    }

    return 0;
}

 

标签:std,读取,配置文件,C++,file,line,include,config,string
From: https://www.cnblogs.com/wangchenggen/p/18197472

相关文章

  • qt的xml读取和使用
    将数据保存文件QByteArrayfileAsByteArray;QFilefile(filename);if(!file.open(QIODevice::WriteOnly)){qDebug()<<"文件未打开.";}file.write(fileAsByteArray);file.close();读取文件QByteArraybyteArray=file.readAll();使用xml分析文件QXmlStre......
  • selenium4中cookie的保存与读取
    selenium4中网页cookie的保存与读取importjsonfromseleniumimportwebdriverdriver=webdriver.Edge()url='https://baidu.com'driver.get(url)保存当前网页的cookiedefsavecks():cookies=driver.get_cookies()jscookies=json.dumps(cookies)......
  • C++ 初始化列表(Initialization List)
    请注意以下继承体系中各class的constructors写法:1classCPoint2{3public:4CPoint(floatx=0.0)5:_x(x){}67floatx(){return_x;}8voidx(floatxval){_x=xval;}9protected:10float_x;11};1213classCPoint2d:......
  • 关于“error: Microsoft Visual C++ 14.0 is required. Get it with "Build Tools for
    安装库之前一定要注意python版本,今天鬼迷日眼的装一堆堆库,一个回车冒出来这个鬼问题。百度无果后灵光乍现,只安装报错时对应的库:condainstallnumpy==1.20.1结果在输出里找出这一段:Specifications:-numpy==1.20.1->python[version='>=3.7,<3.8.0a0|>=3.8,<3.9.0a0|>=......
  • P2 C++ 编程范式
    章节链接代码链接目录2.1.1C++工程的一般组织结构2.1.2C++工程在机器人中的组织结构2.2C++代码的编译2.2.1g++编译2.2.2make编译✅2.2.3CMake编译2.1.1C++工程的一般组织结构一般情况下,C++工程的组织结构是将不同的功能封装在不同的类中,每个类用配套的头文件......
  • 从C到C++
    const关键字用法(1)定义常量#include<iostream>usingnamespacestd;intmain(){ constintMAX_VAL=23; constdoublePi=3.14; constchar*SHOOL_NAME="SDWU"; return0;}(2)定义常量指针不可以通过常量指针修改指向的内容.#include<iostream>usingnam......
  • C++_交叉编译和pybind11
    编译本地编译和交叉编译本地编译当前平台编译交叉编译交叉编译是指在一个平台上编译另一个平台上运行的代码。在C++中,交叉编译通常涉及以下步骤:安装交叉编译工具链。配置编译环境。使用工具链编译代码。首先,确保安装了交叉编译工具链,例如gcc-arm-l......
  • 利用python脚本批量读取当前目录下所有excle表格中特定的单元格内容
    利用python脚本批量读取当前目录下所有excle表格中特定的单元格内容importosfromopenpyxlimportload_workbook#设置要读取的单元格地址cell_address='N18'#遍历当前目录下的所有文件forfilenameinos.listdir('.'):iffilename.endswith(......
  • Springboot配置文件Properties密码加密
    1.添加依赖<dependency><groupId>com.github.ulisesbocchio</groupId><artifactId>jasypt-spring-boot-starter</artifactId><version>3.0.3</version></dependency>2.启动类添加注解@EnableEncryptableProperties......
  • C++模板编程-enable_if
    std::enable_if的使用对于重载的函数或者函数模板的选择上,编译器内部有一个自己的规则,并不是简单粗暴的对函数就优先选择,对函数模板就靠后选择替换失败并不是一个错误(SFINAE):SubstitutionFailureIsNotAnError,SFINAE看成是C++语言的一种特性或者说一种模板设计中要遵循的......