c++标准库读写ini文件的实现示例
作者:hylreg
本文介绍了一个完整的INI文件类的实现,包含读取和写入操作,通过IniFile.h头文件和IniFile.cpp实现文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
下面是一个完整的 INI 文件类的实现,包括读取和写入 INI 文件的功能。
1. IniFile.h 头文件
#ifndef INIFILE_H
#define INIFILE_H
#include <string>
#include <map>
#include <fstream>
#include <sstream>
class IniFile {
public:
// 构造函数,接受文件名
IniFile(const std::string& filename);
// 加载 INI 文件内容
void load();
// 保存内容到 INI 文件
void save() const;
// 获取指定节的键值
std::string getValue(const std::string& section, const std::string& key, const std::string& defaultValue = "") const;
// 设置指定节的键值
void setValue(const std::string& section, const std::string& key, const std::string& value);
private:
std::string filename; // INI 文件名
std::map<std::string, std::map<std::string, std::string>> data; // 存储节和键值对
};
#endif // INIFILE_H
2. IniFile.cpp 实现文件
#include "IniFile.h"
#include <iostream>
#include <stdexcept>
#include <algorithm>
// 构造函数,接受文件名
IniFile::IniFile(const std::string& filename) : filename(filename) {
load(); // 加载文件内容
}
// 加载 INI 文件内容
void IniFile::load() {
std::ifstream file(filename);
if (!file.is_open()) {
throw std::runtime_error("Could not open file: " + filename);
}
std::string line;
std::string currentSection;
while (std::getline(file, line)) {
// 去掉注释和空行
line.erase(std::remove_if(line.begin(), line.end(), [](unsigned char c) { return std::isspace(c); }), line.end());
if (line.empty() || line[0] == ';') {
continue; // 跳过空行和注释行
}
// 处理节
if (line[0] == '[') {
auto endPos = line.find(']');
if (endPos != std::string::npos) {
currentSection = line.substr(1, endPos - 1); // 获取节名称
}
continue;
}
// 处理键值对
auto delimiterPos = line.find('=');
if (delimiterPos != std::string::npos) {
std::string key = line.substr(0, delimiterPos);
std::string value = line.substr(delimiterPos + 1);
data[currentSection][key] = value; // 存储键值对
}
}
file.close();
}
// 保存内容到 INI 文件
void IniFile::save() const {
std::ofstream file(filename);
if (!file.is_open()) {
throw std::runtime_error("Could not open file: " + filename);
}
for (const auto& section : data) {
file << "[" << section.first << "]\n"; // 写入节
for (const auto& kv : section.second) {
file << kv.first << "=" << kv.second << "\n"; // 写入键值对
}
file << "\n"; // 节与节之间空行
}
file.close();
}
// 获取指定节的键值
std::string IniFile::getValue(const std::string& section, const std::string& key, const std::string& defaultValue) const {
auto sectionIt = data.find(section);
if (sectionIt != data.end()) {
auto keyIt = sectionIt->second.find(key);
if (keyIt != sectionIt->second.end()) {
return keyIt->second; // 返回找到的值
}
}
return defaultValue; // 返回默认值
}
// 设置指定节的键值
void IniFile::setValue(const std::string& section, const std::string& key, const std::string& value) {
data[section][key] = value; // 设置值
}
3. 使用示例
下面是如何使用 IniFile 类的示例代码:
#include <iostream>
#include "IniFile.h"
int main() {
try {
// 创建 IniFile 对象并加载 INI 文件
IniFile ini("config.ini");
// 获取键值
std::string username = ini.getValue("UserSettings", "Username", "defaultUser");
std::cout << "Username: " << username << std::endl;
// 设置新的键值
ini.setValue("UserSettings", "Username", "newUser");
// 保存到 INI 文件
ini.save();
std::cout << "New username saved!" << std::endl;
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
return 0;
}
4. 说明
- 文件格式:INI 文件格式简单,由节(以 [] 包围)和键值对(key=value)组成。
- 加载:在 load() 方法中读取文件,解析节和键值对。
- 保存:在 save() 方法中将数据写回文件。
- 错误处理:提供基本的错误处理,若文件无法打开则抛出异常。
这个实现可以满足一般的 INI 文件读写需求,可以根据需要进一步扩展功能。
到此这篇关于c++标准库读写ini文件的实现示例的文章就介绍到这了,更多相关c++写ini文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
