C++如何实现一个简单的INI配置文件解析器?(代码示例)

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

c++如何实现一个简单的ini配置文件解析器?(代码示例)

用 C++ 实现一个简单的 INI 解析器,核心是按行读取、识别节([section])、键值对(key=value)和注释,并把数据存进内存结构中。不需要依赖第三方库,标准库

就够了。

基本数据结构设计

INI 文件本质是“节 → 键 → 值”的三层映射。用嵌套 map 最直观:

std::map> config;外层 key 是节名(如 "database"),内层 key 是配置项名(如 "host"),value 是字符串值(如 "127.0.0.1")当前节名用一个 std::string current_section 记录,初始为空,遇到 [xxx] 时更新

逐行解析关键逻辑

每行做三件事:去首尾空格、跳过空行和注释、判断类型:

注释行:以 #; 开头(忽略前面空格)→ 直接跳过节定义行:匹配正则 ^[([^]]+)]$ 或手动查找 '['']' → 提取中间字符串作为新节名键值行:包含 '=' 且不在开头/结尾 → 左边是 key(去空格),右边是 value(去首尾空格,保留中间空格)其他行(如无等号、只有 key)一律忽略,不报错——保持简单鲁棒性

完整可运行示例代码

以下是一个轻量、无异常、无外部依赖的实现(支持 Windows/Linux 换行):

立即学习“C++免费学习笔记(深入)”;

#include #include #include #include #include #include 

// 去首尾空格std::string trim(const std::string& s) {size_t start = s.find_first_not_of(" trn");if (start == std::string::npos) return "";size_t end = s.find_last_not_of(" trn");return s.substr(start, end - start + 1);}

class IniParser {public:std::map<std::string, std::map> data;std::string current_section;

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 文件

你可以新建一个 config.ini 测试:

# 数据库配置[database]host = 127.0.0.1port = 3306user = rootpassword = secret

; 应用设置[app]debug = truelog_level = infotimeout = 30

基本上就这些。它不支持转义、不支持多行值、不验证语法,但覆盖了 95% 的日常 INI 使用场景。想扩展也很容易:加个 save() 方法反向写入,或用 std::vector 支持重复键,或封装成支持 const 迭代器的类。简单不等于简陋,关键是先跑起来、再按需迭代。

以上就是C++如何实现一个简单的INI配置文件解析器?(代码示例)的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1489306.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月19日 12:17:58
下一篇 2025年12月19日 12:18:12

相关推荐

发表回复

登录后才能评论
关注微信