C++轻量级INI解析器使用标准库实现:按行读取文件,识别节名([section])、键值对(key=value),跳过注释与空行,自动trim两端空格,用嵌套map存储配置,支持config"section"访问。

用C++写一个轻量级INI解析器,核心在于:按行读取、识别节([section])、键值对(key=value)、忽略注释与空行,并支持基础的字符串去空格和转义。不需要第三方库,标准库 <fstream></fstream>、<string></string>、<map></map> 就够用。
典型INI格式如下:
[database] host = 127.0.0.1 port = 3306 ; 这是注释 user = admin <p>[logging] level = debug enabled = true
关键规则:
[database])host = 127.0.0.1)用嵌套 map 表达层级关系:
立即学习“C++免费学习笔记(深入)”;
std::map<:string std::map std::string>> config;</:string>"database"),内层是键值对("host" → "127.0.0.1")config["database"]["port"] 得到 "3306"(字符串)如果需要类型安全转换(比如转 int/bool),可额外封装 get_int()、get_bool() 方法,内部调用 std::stoi 或字符串比对。
重点在三步处理:去首尾空格、跳过注释/空行、拆分键值。
std::getline() 按行读取文件trim(const std::string& s):用 s.find_first_not_of(" \t") 和 s.find_last_not_of(" \t") 截取有效子串line.starts_with("[") && line.ends_with("]")(C++20);老标准可用 line[0]=='[' && line.back()==']',再 trim 内容'=',左边 trim 后是 key,右边 trim 后是 value;注意等号可能不存在,需判空以下是最简可用版本,无异常处理但逻辑清晰:
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <algorithm>
<p>std::string trim(const std::string& s) {
size_t l = s.find_first_not_of(" \t\r\n");
if (l == std::string::npos) return "";
size_t r = s.find_last_not_of(" \t\r\n");
return s.substr(l, r - l + 1);
}</p><p>int main() {
std::ifstream file("config.ini");
if (!file.is_open()) {
std::cerr << "无法打开 config.ini\n";
return 1;
}</p><pre class='brush:php;toolbar:false;'>std::map<std::string, std::map<std::string, std::string>> config;
std::string section, line;
while (std::getline(file, line)) {
line = trim(line);
if (line.empty() || line[0] == ';' || line[0] == '#') continue;
if (line[0] == '[' && line.back() == ']') {
section = trim(line.substr(1, line.length() - 2));
} else if (line.find('=') != std::string::npos) {
size_t pos = line.find('=');
std::string key = trim(line.substr(0, pos));
std::string value = trim(line.substr(pos + 1));
if (!section.empty() && !key.empty()) {
config[section][key] = value;
}
}
}
// 示例输出
for (const auto& sec : config) {
std::cout << "[" << sec.first << "]\n";
for (const auto& kv : sec.second) {
std::cout << " " << kv.first << " = " << kv.second << "\n";
}
}}
编译运行前,把上面内容存为 config.ini 放在同一目录即可验证。
基本上就这些。不复杂但容易忽略 trim 和节名为空的边界情况。后续可扩展:支持多行值、内联注释(key=value ; comment)、Unicode、写回保存——但日常配置读取,这个骨架已足够扎实。
以上就是如何用C++写一个INI配置文件解析器?C++文件IO与字符串处理实战【项目练习】的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号