c++ 读取INI文件
1. 一般INI配置文件格式:ini配置文件由节、键、值组成。
【参数】(键=值),例如 :key=value;
【节】:所有的参数都是以节(section)为单位结合在一起的。所有的section名称都是独占一行,并且section名字都被方括号包围着([XXX])。在section声明后的所有parameters都属于该section。
例如:[section1]
所以一个包含节,键,值的简单ini配置文件,例如:
[port]
portName=port1
port=123
2. 使用boost库
Boost库是为C++语言标准库提供扩展的一些C++程序库的总称,由Boost社区组织开发、维护。Boost库可以与C++标准库完美共同工作,并且为其提供扩展功能。
我们使用boost已经封装好的功能就能简单实现ini文件的读取。
下载地址:https://www.boost.org/users/history/version_1_76_0.html
安装配置:非常简单,可以参考https://blog.csdn.net/dumuweiyang/article/details/80826250
3. 实现代码
安装配置好boost之后,我们就可以开始实现读取ini的功能啦~~ 代码非常简短
#include <stdio.h>
#include <string>
#include <boost/regex.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <map>
#include <fstream>
#include <iostream>
using namespace std;void ReadIniFile(string fileName, string& mode)
{ifstream infile;infile.open(fileName);if (!infile.is_open()){printf("Read ini file failed.\n");return;}std::string line, current;// regular expressions to process ini filesboost::regex section_test("\\[(.*?)\\]");boost::regex value_test("(\\w+)=([^\\+]+(?!\\+{3}))");// the result config objectstd::map<std::string, std::map<std::string, std::string> > config;// assuming we've opened the file ok into a// filestream object called "mapfile"while (getline(infile, line)) {boost::trim(line);if (line.length() > 0) {boost::smatch match;if (boost::regex_search(line, match, section_test)) {// any key-value pairs from here to be attributed // to this new namecurrent = match[1];}else if (boost::regex_search(line, match, value_test)) {// set this as a key value pair on the current nameconfig[current][match[1]] = match[2];if (current == "Mode" && match[1] == "Load") {mode = match[2];}}}}
}
void main()
{string mode = "0";// read ini config fileReadIniFile("./cfg.ini", mode);cout << "mode=" << mode << endl;
}
cfg.ini文件内容为:
[Mode]
Load=0
这里代码最终读取到mode=0。
所有的配置都会读取存放在 std::map<std::string, std::map<std::string, std::string> > config中。
c++ 读取INI文件相关推荐
- C# 读取ini文件 百度问问学习文档
C# 读取ini文件 10 有多个section,现想读取整个ini文件和指定section下所有内容 补充: 发布答案可以,请对准题目啊,我不要指定节点的内容,我知道!我要的是读取指定区域的内容,假 ...
- ASP读取ini文件的实现方法
INI是微软Windows操作系统中的文件扩展名.这些字母表示初始化.正如该术语所表示的,INI文件被用来对操作系统或特定程序初始化或进行参数设置.ini文件可以存储很多数据,用来配置应用软件以实现不 ...
- VS2017读取ini 文件中文乱码
[说明] 今天写项目的时候,读取ini文件时候,中文就是乱码了. 测试过,直接输出中文,是没有问题的. 因此问题就确定了是在文件的编码上. (建议各位也像我这么测试下,不然这个解决方案不一定帮得了你) ...
- VS VC 读取 INI文件
1.获取应程序同极目录下的config.ini路劲void GetConfigFilePath(char *path,int len, char *file) {char module[256] = ...
- C++读取ini文件的类
C++读取ini文件的类 取自:http://www.viksoe.dk/code/all_mfc.htm,里面有各种MFC常用的类 // Ini.h: interface for the CIni ...
- java inireader_用Java读取INI文件(带section的)
代码 #include #include int main(){ boost::property_tree::ptree m_pt, tag_setting; read_ini("confi ...
- Qt读取ini文件中文乱码问题
Qt读取ini文件中文乱码问题 方法1: QSettings settings("xxxx.ini",QSettings::IniFormat); settings.setIniC ...
- VB6读取INI文件
开发环境:VB6 程式版本更新, 读取INI文件做对比, 确认版本是否最新. 可用VB6的工具下的API检视员来自动生成API调用代码. INI文件内容: [Version] TEST=2017-11 ...
- QSettings读取ini文件,中文key方法
(吐槽:什么鬼,为啥要用中文当key???我也不知道呀) QSettings读取ini文件,其中key是中文的.例如: [单位] 1=运维检修部1(检修公司) 2=运维检修部2(检修公司)[班组] 1 ...
- 使用 sed / shell 读取 ini 文件
使用 sed / shell 读取 ini 文件 动机 我决定编写一个脚本来进行干净的 macOS (BSD) / Linux 的初始设置. 我认为有一个 tsukkomi 说"现在大多数发 ...
最新文章
- python36中文手册_python_36_文件操作4
- strstr的应用---Hd 2054 A == B ?
- Scala中的类和对象
- [hls]m3u8视频如何处理成加密?
- python按列输出字符_如何根据条件在Pandas中构建新列(新列应输出字符串)
- ASP.NET Core 中的中间件
- C++中之a[5](栈区)和int a=new int[5](堆区)有什么区别
- 必须掌握的八个cmd命令行
- 二叉线索树的线索化以及遍历
- CTO 职场解惑指南系列(一)
- mac系统maven spring mvc小试牛刀
- Vue学习笔记之11-slot插槽
- 特斯拉和SolarCity推出太阳能屋顶瓦片
- linux下安装配置nginx,Linux下安装与配置nginx
- linux centos安装wine qq,ubuntu安装wine QQ
- 腾讯看点App正式下线
- ffmpeg视频转码压缩
- 机房收费系统心得总结
- python英汉互译 谷歌翻译 免费实现调用
- GraphPad Prism使用教程