当前位置: 首页 > news >正文

C++ ini配置文件的简单读取使用

ini文件就是简单的section 下面有对应的键值对
在这里插入图片描述

std::map<std::string, std::map<std::string, std::string>>MyIni::readIniFile() {std::ifstream file(filename);if (!file.is_open()) {std::cerr << "Error: Unable to open file " << filename << std::endl;return iniData;}std::string line;std::string currentSection;while (std::getline(file, line)) {// 移除行首和行尾的空格和制表符line.erase(line.find_last_not_of(" \t") + 1);line.erase(0, line.find_first_not_of(" \t"));//cout << "line:" << line << endl;if (line.empty() || line[0] == ';' || line[0] == '#') {// 空行或注释行,忽略continue;}else if (line[0] == '[' && line[line.length() - 1] == ']') {// 新的节(section)currentSection = line.substr(1, line.length() - 2);//cout <<  "Section" << currentSection.c_str() << endl; }else {// 键值对std::size_t equalsPos = line.find('=');if (equalsPos != std::string::npos) {std::string key = line.substr(0, equalsPos);std::string value = line.substr(equalsPos + 1);iniData[currentSection][key] = value;}}}file.close();return iniData;
}int MyIni::FillReaderInfo(std::string section) {std::map<std::string, std::map<std::string, std::string>>::iterator it;std::map<std::string, std::string>::iterator it1;std::map<std::string, std::string> ma;it = iniData.find(section);if (it == iniData.end()){cout << "not find section " << section << endl;return -1;}else {cout << "find section " << section << endl;for (it1 = it->second.begin(); it1 != it->second.end(); it1++){ma[it1->first] = it1->second;}}#if 0for (it1 = ma.begin(); it1 != ma.end(); it1++){cout << it1->first << "=" << it1->second << endl;}
#endifit1 = ma.find("chLineNumber");if (it1 == ma.end()){cout << "not find chLineNumber" << endl;return -1;}
}
//注意此例子文件打开格式要为LF

}
在这里插入图片描述
可以用noteped++打开修改,windows下面的需要稍微进行处理,去除末尾的\r \n两个字符

http://www.lryc.cn/news/252742.html

相关文章:

  • 【稳定检索|投稿优惠】2024年经济管理与安全科学国际学术会议(EMSSIC 2024)
  • 什么是网站?
  • pg_stat_replication.state 含义
  • JavaWeb(六)
  • GPIO的使用--时钟使能含义--代码封装
  • 最小化安装 Neokylin7.0 用于搭建 Hadoop 集群
  • 苍穹外卖面试题-中
  • Python 重要数据类型
  • 03、pytest初体验
  • 智能指针及强相关知识经验总结 --- 移动语义、引用计数、循环引用、move()、自定义删除器等
  • Gson 自动生成适配器插件
  • React创建项目
  • Redis5新特性-stream
  • 删除PPT文件的备注内容
  • 2023年亚太杯APMCM数学建模大赛B题玻璃温室小气候调控
  • Oracle 查询语句限制只选择最前面几行,和最后面几行的实现方式。
  • .NET Core6.0 MVC+layui+SqlSugar 简单增删改查
  • 在 Mac 上使用浅色或深色外观
  • 华为手环关闭智能适时测量
  • 1-Hadoop原理与技术
  • YoloV5改进策略:Swift Parameter-free Attention,无参注意力机制,超分模型的完美迁移
  • DAPP开发【04】测试驱动开发
  • Raspberry Pi 2, 2 of n - Pi 作为 IoT 消息代理
  • linux服务器环境搭建(使用yum 安装mysql、jdk、redis)
  • 互联网Java工程师面试题·Spring Boot篇·第二弹
  • 【西南交大swjtu微机与接口技术实验】D/A变换实验实验三:波形发生器
  • 【每日一题】从二叉搜索树到更大和树
  • @Scheduled,Quartz,XXL-JOB三种定时任务总结
  • 开会做笔记的时候用什么软件比较好?
  • HTML CSS JavaScript的网页设计