欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c++標(biāo)準(zhǔn)庫(kù)讀寫(xiě)ini文件的實(shí)現(xiàn)示例

 更新時(shí)間:2024年10月25日 08:49:19   作者:hylreg  
本文介紹了一個(gè)完整的INI文件類的實(shí)現(xiàn),包含讀取和寫(xiě)入操作,通過(guò)IniFile.h頭文件和IniFile.cpp實(shí)現(xiàn)文件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

下面是一個(gè)完整的 INI 文件類的實(shí)現(xiàn),包括讀取和寫(xiě)入 INI 文件的功能。

1. IniFile.h 頭文件

#ifndef INIFILE_H
#define INIFILE_H

#include <string>
#include <map>
#include <fstream>
#include <sstream>

class IniFile {
public:
    // 構(gòu)造函數(shù),接受文件名
    IniFile(const std::string& filename);

    // 加載 INI 文件內(nèi)容
    void load();

    // 保存內(nèi)容到 INI 文件
    void save() const;

    // 獲取指定節(jié)的鍵值
    std::string getValue(const std::string& section, const std::string& key, const std::string& defaultValue = "") const;

    // 設(shè)置指定節(jié)的鍵值
    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;  // 存儲(chǔ)節(jié)和鍵值對(duì)
};

#endif // INIFILE_H

2. IniFile.cpp 實(shí)現(xiàn)文件

#include "IniFile.h"
#include <iostream>
#include <stdexcept>
#include <algorithm>

// 構(gòu)造函數(shù),接受文件名
IniFile::IniFile(const std::string& filename) : filename(filename) {
    load();  // 加載文件內(nèi)容
}

// 加載 INI 文件內(nèi)容
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;  // 跳過(guò)空行和注釋行
        }

        // 處理節(jié)
        if (line[0] == '[') {
            auto endPos = line.find(']');
            if (endPos != std::string::npos) {
                currentSection = line.substr(1, endPos - 1);  // 獲取節(jié)名稱
            }
            continue;
        }

        // 處理鍵值對(duì)
        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;  // 存儲(chǔ)鍵值對(duì)
        }
    }

    file.close();
}

// 保存內(nèi)容到 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";  // 寫(xiě)入節(jié)
        for (const auto& kv : section.second) {
            file << kv.first << "=" << kv.second << "\n";  // 寫(xiě)入鍵值對(duì)
        }
        file << "\n";  // 節(jié)與節(jié)之間空行
    }

    file.close();
}

// 獲取指定節(jié)的鍵值
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;  // 返回默認(rèn)值
}

// 設(shè)置指定節(jié)的鍵值
void IniFile::setValue(const std::string& section, const std::string& key, const std::string& value) {
    data[section][key] = value;  // 設(shè)置值
}

3. 使用示例

下面是如何使用 IniFile 類的示例代碼:

#include <iostream>
#include "IniFile.h"

int main() {
    try {
        // 創(chuàng)建 IniFile 對(duì)象并加載 INI 文件
        IniFile ini("config.ini");

        // 獲取鍵值
        std::string username = ini.getValue("UserSettings", "Username", "defaultUser");
        std::cout << "Username: " << username << std::endl;

        // 設(shè)置新的鍵值
        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. 說(shuō)明

  • 文件格式:INI 文件格式簡(jiǎn)單,由節(jié)(以 [] 包圍)和鍵值對(duì)(key=value)組成。
  • 加載:在 load() 方法中讀取文件,解析節(jié)和鍵值對(duì)。
  • 保存:在 save() 方法中將數(shù)據(jù)寫(xiě)回文件。
  • 錯(cuò)誤處理:提供基本的錯(cuò)誤處理,若文件無(wú)法打開(kāi)則拋出異常。

這個(gè)實(shí)現(xiàn)可以滿足一般的 INI 文件讀寫(xiě)需求,可以根據(jù)需要進(jìn)一步擴(kuò)展功能。

到此這篇關(guān)于c++標(biāo)準(zhǔn)庫(kù)讀寫(xiě)ini文件的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)c++寫(xiě)ini文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論