C++ INI解析器用嵌套map存储“节→键→值”,逐行读取并处理注释、节定义和键值对,支持trim、get、get_int等接口。

用 C++ 实现一个简单的 INI 解析器,核心是按行读取、识别节([section])、键值对(key=value)和注释,并把数据存进内存结构中。不需要依赖第三方库,标准库 <iostream>、<string>、<map> 和 <fstream> 就够了。
基本数据结构设计
INI 文件本质是“节 → 键 → 值”的三层映射。用嵌套 map 最直观:
std::map<std::string, std::map<std::string, std::string>> config;- 外层 key 是节名(如
"database"),内层 key 是配置项名(如"host"),value 是字符串值(如"127.0.0.1") - 当前节名用一个
std::string current_section记录,初始为空,遇到[xxx]时更新
逐行解析关键逻辑
每行做三件事:去首尾空格、跳过空行和注释、判断类型:
- 注释行:以
#或;开头(忽略前面空格)→ 直接跳过 - 节定义行:匹配正则
^\[([^\]]+)\]$或手动查找'['和']'→ 提取中间字符串作为新节名 - 键值行:包含
'='且不在开头/结尾 → 左边是 key(去空格),右边是 value(去首尾空格,保留中间空格) - 其他行(如无等号、只有 key)一律忽略,不报错——保持简单鲁棒性
完整可运行示例代码
以下是一个轻量、无异常、无外部依赖的实现(支持 Windows/Linux 换行):
立即学习“C++免费学习笔记(深入)”;
#include <iostream>
#include <string>
#include <map>
#include <fstream>
#include <cctype>
#include <algorithm>
<p>// 去首尾空格
std::string trim(const std::string& s) {
size_t start = s.find_first_not_of(" \t\r\n");
if (start == std::string::npos) return "";
size_t end = s.find_last_not_of(" \t\r\n");
return s.substr(start, end - start + 1);
}</p><p>class IniParser {
public:
std::map<std::string, std::map<std::string, std::string>> data;
std::string current_section;</p><pre class='brush:php;toolbar:false;'>bool load(const std::string& filename) {
std::ifstream file(filename);
if (!file.is_open()) return false;
std::string line;
int line_num = 0;
while (std::getline(file, line)) {
line_num++;
line = trim(line);
if (line.empty() || line[0] == '#' || line[0] == ';') continue;
// 匹配 [section]
if (line[0] == '[' && line.back() == ']') {
std::string sec = trim(line.substr(1, line.length()-2));
if (!sec.empty()) current_section = sec;
continue;
}
// 匹配 key=value
size_t eq_pos = line.find('=');
if (eq_pos != std::string::npos) {
std::string key = trim(line.substr(0, eq_pos));
std::string value = trim(line.substr(eq_pos + 1));
if (!key.empty() && !current_section.empty()) {
data[current_section][key] = value;
}
}
}
return true;
}
// 获取字符串值,未找到返回默认值
std::string get(const std::string& section, const std::string& key, const std::string& def = "") const {
auto sec_it = data.find(section);
if (sec_it == data.end()) return def;
auto key_it = sec_it->second.find(key);
if (key_it == sec_it->second.end()) return def;
return key_it->second;
}
// 获取整数(简单转换,失败返回默认值)
int get_int(const std::string& section, const std::string& key, int def = 0) const {
std::string s = get(section, key);
if (s.empty()) return def;
try {
return std::stoi(s);
} catch (...) {
return def;
}
}};
// 使用示例 int main() { IniParser ini; if (!ini.load("config.ini")) { std::cerr << "Failed to load config.ini\n"; return 1; }
std::cout << "Host: " << ini.get("database", "host", "localhost") << "\n";
std::cout << "Port: " << ini.get_int("database", "port", 3306) << "\n";
std::cout << "Debug: " << ini.get("app", "debug", "false") << "\n";}
配套示例 config.ini 文件
你可以新建一个 config.ini 测试:
# 数据库配置 [database] host = 127.0.0.1 port = 3306 user = root password = secret <p>; 应用设置 [app] debug = true log_level = info timeout = 30</p>
基本上就这些。它不支持转义、不支持多行值、不验证语法,但覆盖了 95% 的日常 INI 使用场景。想扩展也很容易:加个 save() 方法反向写入,或用 std::vector 支持重复键,或封装成支持 const 迭代器的类。简单不等于简陋,关键是先跑起来、再按需迭代。










