亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

下面是一個完整的 INI 文件類的實現(xiàn),包括讀取和寫入 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;  // 存儲節(jié)和鍵值對
};

#endif // INIFILE_H

2. IniFile.cpp 實現(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;  // 跳過空行和注釋行
        }

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

// 保存內(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";  // 寫入節(jié)
        for (const auto& kv : section.second) {
            file << kv.first << "=" << kv.second << "\n";  // 寫入鍵值對
        }
        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 對象并加載 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. 說明

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

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

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

相關(guān)文章

  • C語言實現(xiàn)中國象棋

    C語言實現(xiàn)中國象棋

    這篇文章主要為大家詳細(xì)介紹了C語言實現(xiàn)中國象棋,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • 淺談C語言中strcpy,strcmp,strlen,strcat函數(shù)原型

    淺談C語言中strcpy,strcmp,strlen,strcat函數(shù)原型

    下面小編就為大家?guī)硪黄獪\談C語言中strcpy,strcmp,strlen,strcat函數(shù)原型。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • c/c++中struct定義、聲明、對齊方式解析

    c/c++中struct定義、聲明、對齊方式解析

    這篇文章通過C/C++的兩種聲明方式開始,給大家詳細(xì)分析了/c+中struct定義、聲明、對齊方式,對此有興趣的朋友可以參考學(xué)習(xí)下。
    2018-03-03
  • C語言實現(xiàn)兩個遞減數(shù)列中尋找某一個數(shù)

    C語言實現(xiàn)兩個遞減數(shù)列中尋找某一個數(shù)

    這篇文章主要介紹了C語言實現(xiàn)兩個遞減數(shù)列中尋找某一個數(shù),是一類經(jīng)典的數(shù)組操作算法,需要的朋友可以參考下
    2014-09-09
  • VC6.0常見編譯錯誤提示附解決方法

    VC6.0常見編譯錯誤提示附解決方法

    這篇文章主要介紹了VC++6.0編譯過程中常遇到的一些錯誤提示并給出了錯誤原因與分析,需要的朋友尅參考下
    2013-07-07
  • 詳解C語言中index()函數(shù)和rindex()函數(shù)的用法

    詳解C語言中index()函數(shù)和rindex()函數(shù)的用法

    這篇文章主要介紹了C語言中index()函數(shù)和rndex()函數(shù)的用法,是C語言入門學(xué)習(xí)中的基礎(chǔ)知識,要的朋友可以參考下
    2015-08-08
  • 基于堆的基本操作的介紹

    基于堆的基本操作的介紹

    本篇文章對堆的基本操作進(jìn)行了詳細(xì)的分析介紹。需要的朋友參考下
    2013-05-05
  • C語言實現(xiàn)掃雷游戲(可展開)

    C語言實現(xiàn)掃雷游戲(可展開)

    這篇文章主要為大家詳細(xì)介紹了C語言實現(xiàn)掃雷游戲,實現(xiàn)掃雷展開和提醒,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-03-03
  • 使用boost讀取XML文件詳細(xì)介紹

    使用boost讀取XML文件詳細(xì)介紹

    這篇文章主要介紹了使用boost讀取XML文件詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下
    2016-11-11
  • 如何高效移除C++關(guān)聯(lián)容器中的元素

    如何高效移除C++關(guān)聯(lián)容器中的元素

    關(guān)聯(lián)容器和順序容器有著很大不同,關(guān)聯(lián)容器中的元素是按照關(guān)鍵字來保存和訪問的,而順序容器中的元素是按它們在容器中的位置來順序保存和訪問的,本文介紹了如何高效移除C++關(guān)聯(lián)容器中的元素的方法,需要的朋友可以參考下
    2025-04-04

最新評論