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

用 C++ 实现一个简单的 INI 解析器,核心是按行读取、识别节([section])、键值对(key=value)和注释,并把数据存进内存结构中。不需要依赖第三方库,标准库 <iostream></iostream>、<string></string>、<map></map> 和 <fstream></fstream> 就够了。
INI 文件本质是“节 → 键 → 值”的三层映射。用嵌套 map 最直观:
std::map<:string std::map std::string>> config;</:string>"database"),内层 key 是配置项名(如 "host"),value 是字符串值(如 "127.0.0.1")std::string current_section 记录,初始为空,遇到 [xxx] 时更新每行做三件事:去首尾空格、跳过空行和注释、判断类型:
# 或 ; 开头(忽略前面空格)→ 直接跳过^\[([^\]]+)\]$ 或手动查找 '[' 和 ']' → 提取中间字符串作为新节名'=' 且不在开头/结尾 → 左边是 key(去空格),右边是 value(去首尾空格,保留中间空格)以下是一个轻量、无异常、无外部依赖的实现(支持 Windows/Linux 换行):
#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
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 测试:
# 数据库配置 [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 迭代器的类。简单不等于简陋,关键是先跑起来、再按需迭代。
以上就是C++如何实现一个简单的INI配置文件解析器?(代码示例)的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号