This Halloween Pikachu follows your mouse or screen taps

C++ 读写ini文件

C++ 实现读写ini格式文件。用gpt生成的。目前剔出了注释的影响,对异常进行了处理。读写也没有什么问题。

实现的逻辑是逐行读取,判断setion靠 '[' 和']',键值对就靠'='

源代码:

#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <stdexcept>

class IniFile {
public:
    IniFile(const std::string& filename) : filename(filename) {
        parseIniFile();
    }

    std::string getValue(const std::string& section, const std::string& key) const {
        std::string sectionKey = section + "." + key;
        if (data.count(sectionKey) == 0) {
            throw std::runtime_error("Key not found: " + sectionKey);
        }
        return data.at(sectionKey);
    }

    void setValue(const std::string& section, const std::string& key, const std::string& value) {
        std::string sectionKey = section + "." + key;
        data[sectionKey] = value;
    }

    void printAllValues() const {
        for (const auto& pair : data) {
            std::cout << pair.first << " = " << pair.second << std::endl;
        }
    }

private:
    std::string filename;
    std::map<std::string, std::string> data;

    void parseIniFile() {
        std::ifstream file(filename);
        if (!file) {
            throw std::runtime_error("Failed to open file: " + filename);
        }

        std::string line;
        std::string currentSection;
        while (std::getline(file, line)) {
            line = removeComments(line);
            line = removeWhitespace(line);

            if (line.empty()) {
                continue;
            }

            if (line[0] == '[' && line[line.length() - 1] == ']') {
                currentSection = line.substr(1, line.length() - 2);
            } else {
                std::size_t equalPos = line.find('=');
                if (equalPos != std::string::npos) {
                    std::string key = line.substr(0, equalPos);
                    std::string value = line.substr(equalPos + 1);
                    std::string sectionKey = currentSection + "." + key;
                    data[sectionKey] = value;
                }
            }
        }
    }

    std::string removeComments(const std::string& line) const {
        std::size_t commentPos = line.find(';');
        if (commentPos != std::string::npos) {
            return line.substr(0, commentPos);
        }
        return line;
    }

    std::string removeWhitespace(const std::string& line) const {
        std::string result;
        for (char c : line) {
            if (!std::isspace(c)) {
                result += c;
            }
        }
        return result;
    }
};

用例:

int main() {
    try {
        IniFile parser("example.ini");
        parser.printAllValues(); //打印所有数据
        std::cout << "Value of Key1 in Section1: " << parser.getValue("Section1", "Key1") << std::endl;
        parser.setValue("Section2", "Key3", "Value3");
        parser.printAllValues();
    } catch (const std::exception& e) {
        std::cerr << "Error: " << e.what() << std::endl;
    }
    return 0;
}
posted @   图图雷  阅读(599)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示