c++標(biāo)準(zhǔn)庫(kù)讀寫(xiě)ini文件的實(shí)現(xiàn)示例
下面是一個(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)文章
淺談C語(yǔ)言中strcpy,strcmp,strlen,strcat函數(shù)原型
下面小編就為大家?guī)?lái)一篇淺談C語(yǔ)言中strcpy,strcmp,strlen,strcat函數(shù)原型。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04C語(yǔ)言實(shí)現(xiàn)兩個(gè)遞減數(shù)列中尋找某一個(gè)數(shù)
這篇文章主要介紹了C語(yǔ)言實(shí)現(xiàn)兩個(gè)遞減數(shù)列中尋找某一個(gè)數(shù),是一類經(jīng)典的數(shù)組操作算法,需要的朋友可以參考下2014-09-09詳解C語(yǔ)言中index()函數(shù)和rindex()函數(shù)的用法
這篇文章主要介紹了C語(yǔ)言中index()函數(shù)和rndex()函數(shù)的用法,是C語(yǔ)言入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),要的朋友可以參考下2015-08-08C語(yǔ)言實(shí)現(xiàn)掃雷游戲(可展開(kāi))
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)掃雷游戲,實(shí)現(xiàn)掃雷展開(kāi)和提醒,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03