国产亚洲精品福利在线无卡一,国产精久久一区二区三区,亚洲精品无码国模,精品久久久久久无码专区不卡

當(dāng)前位置: 首頁 > news >正文

網(wǎng)站集群系統(tǒng)建設(shè)網(wǎng)站宣傳方法

網(wǎng)站集群系統(tǒng)建設(shè),網(wǎng)站宣傳方法,做網(wǎng)站有哪個軟件好,win2012安裝wordpress最近在學(xué)習(xí)時,發(fā)現(xiàn)自己還不會操作ini文件,想著以前工作時接觸到的項目或多或少都要用到ini文件去保存初始化程序的數(shù)據(jù);所以趕緊去網(wǎng)上搜索以下C/C操作ini文件都有些什么庫可以玩玩;搜索到有: 1. inih:這是…

最近在學(xué)習(xí)時,發(fā)現(xiàn)自己還不會操作ini文件,想著以前工作時接觸到的項目或多或少都要用到ini文件去保存初始化程序的數(shù)據(jù);所以趕緊去網(wǎng)上搜索以下C/C++操作ini文件都有些什么庫可以玩玩;搜索到有:

1. inih:這是C語言小巧的庫,更適合嵌入式開發(fā);

2. iniparser:這是C語言的庫,挺方便使用的,開源,兩個.h文件和兩個.c文件,但只能在Linux中使用;

3. simpleini:這是C++的庫,挺方便使用的,跨平臺,開源,就兩個.h文件和一個.c文件,且支持中文;

所以最后我選擇了simpleini這個庫去學(xué)習(xí)使用!


目錄

一、介紹

1. ini介紹

2. simpleini介紹

二、下載

三、使用

1. 加載ini文件

2. 簡單配置

3. 增

1).?添加一個新的節(jié)點(section)

2). 添加一個新的 key和value

4. 改

1).?修改值(value)

5. 刪

1).?刪除 key 和 value

2). 當(dāng)最后一個key也被刪除掉后,section1也會被刪除

3).?刪除整個節(jié)點(section)和其下的所有鍵(key)?

6. 查

1). 將下圖中的ini文件內(nèi)容讀取打印顯示

2). 遍歷ini文件的所有內(nèi)容

3). 遍歷所有節(jié)點(section)

4). 遍歷指定節(jié)點的鍵(key)

5). 獲取一個鍵對應(yīng)多個值?

6).?獲取指定節(jié)點(section)里有多少鍵值

7. 保存

1). 保存到文件?

2). 保存到C++字符串

8. 中文亂碼問題

四、封裝

configdef.h

iniconfig.h

iniconfig.cpp

測試代碼:

五、總結(jié)


一、介紹

1. ini介紹

ini文件由 [section] 節(jié)點key value 構(gòu)成。

例如一個簡單的ini文件如下所示:

[message]
name = 張三
age = 25
height = 173.2; 這是一個注釋[server]
ip = 127.0.0.1
port = 6666

message就是節(jié)點,節(jié)點下方就是它的鍵和值;server也是一個節(jié)點。

如果需要注釋,使用英文分號 ' ; ' 即可。

2. simpleini介紹

一個跨平臺庫,提供了一個簡單的API來讀取和寫入ini風(fēng)格的配置文件。它支持ASCII、MBCS和Unicode格式的數(shù)據(jù)文件。它被明確設(shè)計為可移植到任何平臺,并已在Windows, WinCE和Linux上進(jìn)行了測試。使用MIT許可證作為開源和免費發(fā)布.

功能概述

  • MIT許可允許在所有軟件中免費使用(包括GPL和商業(yè)軟件)
  • 多平臺(Windows 95到Windows 10、Windows CE、Linux、Unix)
  • 加載和保存ini風(fēng)格的配置文件
  • 在所有平臺上,配置文件可以使用任何換行格式
  • 對文件格式的自由接受
  • 沒有section的鍵/值,沒有值的鍵
  • 刪除部分、鍵和值周圍的空白
  • 支持多行值(嵌入換行字符的值)
  • 可選支持同名的多個鍵
  • 可選的不區(qū)分大小寫的節(jié)和鍵(僅針對ASCII字符)
  • 在文件加載時以相同的順序保存部分和鍵
  • 盡可能保留文件、節(jié)和鍵上的注釋
  • 同時支持char或wchar_t編程接口
  • 同時支持MBCS(系統(tǒng)區(qū)域設(shè)置)和UTF-8文件編碼
  • 在Linux/Unix上,系統(tǒng)區(qū)域設(shè)置不需要是UTF-8才能加載UTF-8文件
  • 在節(jié)、鍵、值和注釋中支持非ascii字符
  • 通過用戶編寫的轉(zhuǎn)換器類支持非標(biāo)準(zhǔn)字符類型或文件編碼
  • 支持以編程方式添加/修改值
  • 在大多數(shù)編譯器中應(yīng)該編譯沒有警告

二、下載

GitHub鏈接:GitHub - brofield/simpleini: Cross-platform C++ library providing a simple API to read and write INI-style configuration fileshttps://github.com/brofield/simpleini

gitte鏈接:

simpleini: SimpleIni 是一個跨平臺的 C++ 庫,提供一個簡單的 API 用于操作 ini 配置文件 (gitee.com)https://gitee.com/mirrors/simpleini

下載后解壓

?這三個文件可在 Window 或 Linux 環(huán)境去使用!


三、使用

以下介紹的用法,Linux和Window環(huán)境均可使用!?

?包含頭文件:

#include "SimpleIni.h"

#define FILE_NAME?? ?"./test1.ini"?

test1.ini內(nèi)容如下:

1. 加載ini文件

// 定義ini文檔對象
CSimpleIniA ini;// 加載ini文件
SI_Error rc;
rc = ini.LoadFile(FILE_NAME);	// 另一種方式:SI_Error LoadFile(FILE * a_fpFile);
if (rc < 0) { printf("加載 %s ini 文件失敗!\n", FILE_NAME);return -1;
}

rc返回值有以下這些:

using SI_Error = int;constexpr int SI_OK = 0;        //!< No error
constexpr int SI_UPDATED = 1;   //!< An existing value was updated
constexpr int SI_INSERTED = 2;  //!< A new value was inserted// note: test for any error with (retval < 0)
constexpr int SI_FAIL = -1;     //!< Generic failure
constexpr int SI_NOMEM = -2;    //!< Out of memory error
constexpr int SI_FILE = -3;     //!< File error (see errno for detail error)

2. 簡單配置

// 設(shè)置INI數(shù)據(jù)的存儲格式,參數(shù)為true時保存為UTF-8格式,否則為本地編碼格式
ini.SetUnicode(true);// 是否允許一個關(guān)鍵字對應(yīng)多個值,默認(rèn)為允許;若不允許,則將最后一個值作為此關(guān)鍵字關(guān)聯(lián)的值
ini.SetMultiKey(false);

3. 增

?SetValue

參數(shù)一:節(jié)點

參數(shù)二:鍵

參數(shù)三:值

返回值:SI_Error (也就是int類型)

1).?添加一個新的節(jié)點(section)

// 添加一個新的 section
rc = ini.SetValue("section1", nullptr, nullptr);
if (rc < 0) { printf("添加section1失敗!\n");return -1;
}

?

2). 添加一個新的 key和value

// 添加一個新的 key和value
rc = ini.SetValue("section1", "name", "張三");
if (rc < 0) {printf("添加name失敗!\n");return -1;
}
//const char *name = ini.GetValue("section1", "name", "");
//printf("name = %s\n", name);ini.SetValue("section1", "age", "24");
ini.SetValue("section1", "sex", "男");

注意:如果name存在,則會將name鍵(key)對應(yīng)的值(value)修改為張三;

?還可以使用SetLongValue、SetDoubleValue、SetBoolValue去添加:

ini.SetLongValue("server", "length", 173);
ini.SetDoubleValue("server", "weight", 53.5);
ini.SetBoolValue("server", "vip", true);

4. 改

SetValue

參數(shù)一:節(jié)點

參數(shù)二:鍵

參數(shù)三:值

返回值:SI_Error (也就是int類型)

1).?修改值(value)

// 修改value,如果鍵(name)不存在則添加該 key和value
rc = ini.SetValue("section1", "name", "李四");
if (rc < 0) { printf("修改name失敗!\n");return -1;
}
//const char *name = ini.GetValue("section1", "name");
//printf("name = %s\n", name);

注意:如果要修改的值對應(yīng)的鍵不存在,則會添加改鍵和值到section1節(jié)點中!?

?貌似無法修改節(jié)點(section) 和 鍵(key),我沒有找到相關(guān)的api。。。

?還可以使用SetLongValue、SetDoubleValue、SetBoolValue去添加:

ini.SetLongValue("server", "length", 1000);
ini.SetDoubleValue("server", "weight", 66.66);
ini.SetBoolValue("server", "vip", false);

5. 刪

Delete

參數(shù)一:節(jié)點

參數(shù)二:鍵

返回值:bool?

bool done = false;?

1).?刪除 key 和 value

// 刪除 key
done = ini.Delete("section1", "name");
if (false == done) {printf("刪除 section1 - name 失敗!\n");return -1;
}

2). 當(dāng)最后一個key也被刪除掉后,section1也會被刪除

// 如果最后一個key也被刪除了,那么section也會被一起刪除掉
bool deleteSectionIfEmpty = true;
done = ini.Delete("section1", "age", deleteSectionIfEmpty);
if (false == done) {printf("刪除 section1 - age 失敗!\n");return -1;
}

此時section1中還由兩個key,隨意上面的代碼執(zhí)行后只會將age給刪除掉,并不會也把section1刪掉;

?如果將Delete的第三個參數(shù)值true去刪除sex,那么section1也會一并刪掉!

ini.Delete("section1", "sex", true);

將section1還原到一開始的的樣子 ,方便下面第3點操作刪除

3).?刪除整個節(jié)點(section)和其下的所有鍵(key)?

// 刪除整個section和其中的所有鍵
done = ini.Delete("section1", nullptr);
if (false == done) {printf("刪除整個section和其中的所有鍵 失敗 !\n");return -1;
}

執(zhí)行如上代碼,就會將剛剛還原的section1都給刪除掉!

6. 查

GetValue

參數(shù)一:節(jié)點

參數(shù)二:鍵

參數(shù)三:如果沒找到,返回參數(shù)三指定的默認(rèn)值

返回值:const char *

1). 將下圖中的ini文件內(nèi)容讀取打印顯示

int _int = std::stoi(ini.GetValue("section", "_int", "-1"));
printf("_int = %d\n", _int);long long _long = std::stoll(ini.GetValue("section", "_long", "-1"));
printf("_long = %lld\n", _long);double _double = std::stod(ini.GetValue("section", "_double", "0.0"));
printf("_double = %lf\n", _double);float _float = std::stof(ini.GetValue("section", "_float", "0.0"));
printf("_float = %f\n", _float);bool _bool = ini.GetBoolValue("section", "_bool", false);
printf("_bool = %s\n", _bool ? "true" : "false");std::string _string = ini.GetValue("section", "_string", "");
printf("_string = %s\n", _string.c_str());std::string _string2 = ini.GetValue("section", "_string2", "");
printf("_string2 = %s\n", _string2.c_str());char _char = ini.GetValue("section", "_char", "")[0];
printf("_char = %c\n", _char);std::string ip = ini.GetValue("server", "ip", "0.0.0.0");
printf("ip = %s\n", ip.c_str());int port = std::stoi(ini.GetValue("server", "port", "-1"));
printf("port = %d\n", port);std::string name1 = ini.GetValue("server", "name", "");
printf("name = %s\n", name1.c_str());

還可以使用GetLongValue、GetDoubleValue、GetBoolValue去查:

int lenght = ini.GetLongValue("server", "length", -1);
double weight = ini.GetDoubleValue("server", "weight", -1);
bool vip = ini.GetBoolValue("server", "vip", false);

2). 遍歷ini文件的所有內(nèi)容

?GetAllSections:獲取所有節(jié)點,參數(shù)一引用返回list鏈表;

GetSection:根據(jù)參數(shù)字符串,獲取節(jié)點,返回multimap容器;

CSimpleIniA::TNamesDepend sections;
// get all sections
ini.GetAllSections(sections);	
// 遍歷所有 section 的 key 和 value
for (const auto &it : sections) {const CSimpleIniA::TKeyVal *pKeyVal = ini.GetSection(it.pItem);if (nullptr != pKeyVal) {for (const auto& it : *pKeyVal) {std::cout << it.first.pItem << " = " << it.second << std::endl;}}
}

3). 遍歷所有節(jié)點(section)

CSimpleIniA::TNamesDepend sections1;
// 獲取所有section
ini.GetAllSections(sections1);
// 遍歷所有 sections
for (const auto &it : sections1) {std::cout << it.pItem << std::endl;
}

4). 遍歷指定節(jié)點的鍵(key)

GetAllKeys:獲取所有鍵,參數(shù)二引用返回list鏈表;?

CSimpleIniA::TNamesDepend keys;
// get all keys in a section
ini.GetAllKeys("section", keys);	
// 遍歷 section 指定的所有 key
for (const auto &it : keys) {std::cout << it.pItem << std::endl;
}

5). 獲取一個鍵對應(yīng)多個值?

首先,ini.SetMultiKey(true);得設(shè)置為true,否則只會獲取到最后一個值,其他會被刪除掉;

在ini文件中的server節(jié)點添加多幾個name鍵

?使用以下代碼獲取:

CSimpleIniA::TNamesDepend values;
// 獲取 key 所對應(yīng)的多個 value;ini.SetMultiKey(true);一定要設(shè)置為true,
// 否則就只會獲取到最后一個,其他刪除
ini.GetAllValues("server", "name", values);
// 遍歷一個 key 對應(yīng)多個 value;
for (const auto &it : values) {printf("name = %s\n", it.pItem);	
}

6).?獲取指定節(jié)點(section)里有多少鍵值

// 獲取section里有多少值
int size = ini.GetSectionSize("section");
printf("section 的 key 個數(shù):%d\n", size);

7. 保存

注意:以上增、刪、改,只有執(zhí)行保存代碼后,才會在文件做出相應(yīng)的修改!

1). 保存到文件?

/* 保存到文件中 */
rc = ini.SaveFile(FILE_NAME);
if (rc < 0) { printf("保存 %s ini文件失敗\n", FILE_NAME);
}

2). 保存到C++字符串

std::string strIni = "";
ini.Save(strIni);
printf("%s\n", strIni.c_str());

8. 中文亂碼問題

window環(huán)境寫入或者讀取中文有亂碼現(xiàn)象,將文件編碼改成ANSI編碼即可!

可以使用notepad++去修改,如下圖:

Linux環(huán)境出現(xiàn)中文亂碼問題,那就新建一個文件,然后再手動敲上需要的信息即可,例如
touch test1.ini? ? 或? ? ?vim test1.ini

記得,千萬別從從Window拷貝進(jìn)Linux中,文件中是不會顯示出亂碼,但是讀取寫入時會有亂碼!

我遇到的亂碼問題,通過上面的方法就可以解決了!


四、封裝

可以根據(jù)自己項目的具體需求去封裝成方便調(diào)用的接口去使用!

例如我下面的用法:

configdef.h

這個是定義結(jié)構(gòu)體的頭文件,從ini文件中讀取的數(shù)據(jù)都存放在結(jié)構(gòu)體中!?

#ifndef _COMMON_CONFIGDEF_H_
#define _COMMON_CONFIGDEF_H_#include <string>typedef struct st_env_config {// 對應(yīng)ini文件// sectionint _int;long _long;double _double;float _float;bool _bool;std::string _string;char _char;// serverstd::string _ip;unsigned short _port;// 構(gòu)造函數(shù)st_env_config() { }st_env_config(int _int, long _long, double _double, float _float, bool _bool, std::string _string, char _char, std::string _ip, unsigned short _port) {this->_int = _int;this->_long = _long;this->_double = _double;this->_float = _float;this->_bool = _bool;this->_string = _string;this->_char = _char;this->_ip = _ip;this->_port = _port;}// 賦值運算符重載st_env_config &operator=(const st_env_config &config) {if (this != &config) {this->_int = config._int;this->_long = config._long;this->_double = config._double;this->_float = config._float;this->_bool = config._bool;this->_string = config._string;this->_char = config._char;this->_ip = _ip;this->_port = _port;}return *this;}}_st_env_config;#endif	// _COMMON_CONFIGDEF_H_

iniconfig.h

這個是封裝simpleini的頭文件

#ifndef _COMMON_INICONFIG_H_
#define _COMMON_INICONFIG_H_#include <string>#include "configdef.h"#include "../simpleini/SimpleIni.h"class Iniconfig {
public:Iniconfig();Iniconfig(const std::string &path, st_env_config &config);~Iniconfig();// 加載ini文件bool loadfile(const std::string &path);// 保存ini文件bool saveFile(const std::string &fileName);// 設(shè)置INI數(shù)據(jù)的存儲格式,參數(shù)為true時保存為UTF-8格式,否則為本地編碼格式void setUnicode(const bool utf8 = true);// 是否允許一個關(guān)鍵字對應(yīng)多個值,默認(rèn)為允許;若不允許,則將最后一個值作為此關(guān)鍵字關(guān)聯(lián)的值,其他刪除void setMultiKey(const bool multKey = false);// 獲取ini文件中的數(shù)據(jù),保存到結(jié)構(gòu)體中bool getData(st_env_config &config);// 獲取ini文件字符串std::string getIniStr();// 添加一個新的sectionbool addSection(const std::string &section);// 添加一個新的key和value,value可以默認(rèn)為空bool addValue(const std::string &section, const std::string &key, const std::string &value = "");bool addLongValue(const std::string &section, const std::string &key, const long value = 0);bool addDoubleValue(const std::string &section, const std::string &key, const double value = 0.0);bool addBoolValue(const std::string &section, const std::string &key, const bool value = false);// 修改value,如果key不存在,則會創(chuàng)建key和valuebool setValue(const std::string &section, const std::string &key, const std::string &value);bool setLongValue(const std::string &section, const std::string &key, const long value = 0);bool setDoubleValue(const std::string &section, const std::string &key, const double value = 0.0);bool setBoolValue(const std::string &section, const std::string &key, const bool value = false);// 刪除keybool deleteKey(const std::string &section, const std::string &key);// 刪除key,如果最后一個key也被刪除了,那么section也會被一起刪除掉bool deleteKeys(const std::string &section, const std::string &key, const bool deleteSectionIfEmpty = true);// 刪除section,整個section和其中的所有鍵值bool deleteSection(const std::string &section);// 獲取string類型值std::string getValue(const std::string &section, const std::string &key, const std::string &defualtValue = "");// 獲取char類型值char getValueC(const std::string &section, const std::string &key, const char &defualtValue = '\0');// 獲取long、int、short類型long getLongValue(const std::string &section, const std::string &key, const short &defualtValue = -1);// 獲取double、float類型double getDoubleValue(const std::string &section, const std::string &key, const double &defualtValue = 0.0);// 獲取bool類型bool getBoolValue(const std::string &section, const std::string &key, const bool &defualtValue = false);// 獲取section里有多少值int getSectionSize(const std::string &section);// 遍歷所有void printAll();private:bool _isloaded;		// 是否已經(jīng)加載CSimpleIniA _ini;	// ini操作對象
};#endif	// _COMMON_INICONFIG_H_

iniconfig.cpp

這個是封裝simpleini的cpp文件內(nèi)容

#include "iniconfig.h"#include <stdio.h>
#include <iostream>Iniconfig::Iniconfig() : _isloaded(false) {_ini.SetUnicode(true);		// 使用utf8編碼_ini.SetMultiKey(false);	// 不允許一個key對應(yīng)多個value_isloaded = false;
}Iniconfig::Iniconfig(const std::string & path, st_env_config &config) {_ini.SetUnicode(true);		// 使用utf8編碼_ini.SetMultiKey(false);	// 不允許一個key對應(yīng)多個value_isloaded = false;SI_Error rc;rc = _ini.LoadFile(path.c_str());	// 另一種方式:SI_Error LoadFile(FILE * a_fpFile);if (rc < 0) {printf("加載 %s ini 文件失敗!\n", path.c_str());_isloaded = false;return;} int _int = getLongValue("section", "_int", -1);long _long = getLongValue("section", "_long", -1);double _double = getDoubleValue("section", "_double", 0.0);float _float = getDoubleValue("section", "_float", 0.0);bool _bool = getBoolValue("section", "_bool", false);std::string _string = getValue("section", "_string", "");char _char = getValueC("section", "_char", '\0');std::string ip = getValue("server", "ip", "0.0.0.0");unsigned short port = getLongValue("section", "port", -1);config = st_env_config(_int, _long, _double, _float, _bool, _string, _char, ip, port);_isloaded = true;
}Iniconfig::~Iniconfig() {}// 加載ini文件
bool Iniconfig::loadfile(const std::string &path) {if (false == _isloaded) {SI_Error rc;rc = _ini.LoadFile(path.c_str());	// 另一種方式:SI_Error LoadFile(FILE * a_fpFile);if (rc < 0) {printf("加載 %s ini 文件失敗!\n", path.c_str());_isloaded = false;return _isloaded;}_isloaded = true;} return _isloaded;
}bool Iniconfig::saveFile(const std::string & fileName) {SI_Error rc = _ini.SaveFile(fileName.c_str());if (rc < 0) {printf("保存 %s ini文件失敗\n", fileName.c_str());return false;}_isloaded = false;return true;
}void Iniconfig::setUnicode(const bool utf8) {_ini.SetUnicode(utf8);		// true:使用utf8編碼
}void Iniconfig::setMultiKey(const bool multKey) {_ini.SetMultiKey(multKey);	// false:不允許一個key對應(yīng)多個value
}bool Iniconfig::getData(st_env_config & config) {if (true == _isloaded) {int _int = getLongValue("section", "_int", -1);long _long = getLongValue("section", "_long", -1);double _double = getDoubleValue("section", "_double", 0.0);float _float = getDoubleValue("section", "_float", 0.0);bool _bool = getBoolValue("section", "_bool", false);std::string _string = getValue("section", "_string", "");char _char = getValueC("section", "_char", '\0');std::string ip = getValue("server", "ip", "0.0.0.0");unsigned short port = getLongValue("section", "port", -1);config = st_env_config(_int, _long, _double, _float, _bool, _string, _char, ip, port);return true;}return false;
}std::string Iniconfig::getIniStr() {std::string str = "";if (true == _isloaded) {SI_Error rc = _ini.Save(str);if (rc < 0) {printf("獲取ini文件字符串失敗!\n");str = "";}}return str;
}bool Iniconfig::addSection(const std::string & section) {if (false == _isloaded) { return false; }SI_Error rc = _ini.SetValue(section.c_str(), nullptr, nullptr);if (rc < 0) {printf("添加 %s 節(jié)點失敗!\n", section.c_str());return false;}return true;
}bool Iniconfig::addValue(const std::string & section, const std::string & key, const std::string & value) {if (false == _isloaded) { return false; }SI_Error rc = _ini.SetValue(section.c_str(), key.c_str(), value.c_str());if (rc < 0) {printf("添加 %s key失敗!\n", key.c_str());return false;}return true;
}bool Iniconfig::addLongValue(const std::string & section, const std::string & key, const long value) {if (false == _isloaded) { return false; }SI_Error rc = _ini.SetLongValue(section.c_str(), key.c_str(), value);if (rc < 0) {printf("添加 %s key失敗!\n", key.c_str());return false;}return true;
}bool Iniconfig::addDoubleValue(const std::string & section, const std::string & key, const double value) {if (false == _isloaded) { return false; }SI_Error rc = _ini.SetDoubleValue(section.c_str(), key.c_str(), value);if (rc < 0) {printf("添加 %s key失敗!\n", key.c_str());return false;}return true;
}bool Iniconfig::addBoolValue(const std::string & section, const std::string & key, const bool value) {if (false == _isloaded) { return false; }SI_Error rc = _ini.SetBoolValue(section.c_str(), key.c_str(), value);if (rc < 0) {printf("添加 %s key失敗!\n", key.c_str());return false;}return true;
}bool Iniconfig::setValue(const std::string & section, const std::string & key, const std::string & value) {if (false == _isloaded) { return false; }SI_Error rc = _ini.SetValue(section.c_str(), key.c_str(), value.c_str());if (rc < 0) {printf("修改 %s value失敗!\n", value.c_str());return false;}return true;
}bool Iniconfig::setLongValue(const std::string & section, const std::string & key, const long value) {if (false == _isloaded) { return false; }SI_Error rc = _ini.SetLongValue(section.c_str(), key.c_str(), value);if (rc < 0) {printf("修改 %s key失敗!\n", key.c_str());return false;}return true;
}bool Iniconfig::setDoubleValue(const std::string & section, const std::string & key, const double value) {if (false == _isloaded) { return false; }SI_Error rc = _ini.SetDoubleValue(section.c_str(), key.c_str(), value);if (rc < 0) {printf("修改 %s key失敗!\n", key.c_str());return false;}return true;
}bool Iniconfig::setBoolValue(const std::string & section, const std::string & key, const bool value) {if (false == _isloaded) { return false; }SI_Error rc = _ini.SetBoolValue(section.c_str(), key.c_str(), value);if (rc < 0) {printf("修改 %s key失敗!\n", key.c_str());return false;}return true;
}bool Iniconfig::deleteKey(const std::string & section, const std::string & key) {if (false == _isloaded) { return false; }bool done = false;// 刪除 keydone = _ini.Delete(section.c_str(), key.c_str());if (false == done) {printf("刪除 %s - %s 失敗!\n", section.c_str(), key.c_str());return false;}return true;
}bool Iniconfig::deleteKeys(const std::string & section, const std::string & key, const bool deleteSectionIfEmpty) {if (false == _isloaded) { return false; }bool done = false;done = _ini.Delete(section.c_str(), key.c_str(), deleteSectionIfEmpty);if (false == done) {printf("刪除 %s - %s 失敗!\n", section.c_str(), key.c_str());return false;}return true;
}bool Iniconfig::deleteSection(const std::string & section) {if (false == _isloaded) { return false; }bool done = false;done = _ini.Delete(section.c_str(), nullptr);if (false == done) {printf("刪除整個 %s 和其下的所有 鍵 失敗 !\n", section.c_str());return false;}return true;
}std::string Iniconfig::getValue(const std::string & section, const std::string & key, const std::string & defualtValue) {if (false == _isloaded) { return ""; }return  _ini.GetValue(section.c_str(), key.c_str(), defualtValue.c_str());
}char Iniconfig::getValueC(const std::string & section, const std::string & key, const char & defualtValue) {if (false == _isloaded) { return '\0'; }std::string str = std::to_string(defualtValue);return  _ini.GetValue(section.c_str(), key.c_str(), str.c_str())[0];
}long Iniconfig::getLongValue(const std::string & section, const std::string & key, const short & defualtValue) {if (false == _isloaded) { return -1; }return  _ini.GetLongValue(section.c_str(), key.c_str(), defualtValue);
}double Iniconfig::getDoubleValue(const std::string & section, const std::string & key, const double & defualtValue) {if (false == _isloaded) { return -1.0; }return  _ini.GetDoubleValue(section.c_str(), key.c_str(), defualtValue);
}bool Iniconfig::getBoolValue(const std::string & section, const std::string & key, const bool & defualtValue) {if (false == _isloaded) { return false; }return  _ini.GetBoolValue(section.c_str(), key.c_str(), defualtValue);
}int Iniconfig::getSectionSize(const std::string & section) {if (false == _isloaded) { return -1; }return _ini.GetSectionSize(section.c_str());
}void Iniconfig::printAll() {CSimpleIniA::TNamesDepend sections;// get all sections_ini.GetAllSections(sections);// 遍歷所有 section 的 key 和 valuefor (const auto &it : sections) {const CSimpleIniA::TKeyVal *pKeyVal = _ini.GetSection(it.pItem);if (nullptr != pKeyVal) {for (const auto& it : *pKeyVal) {std::cout << it.first.pItem << " = " << it.second << std::endl;}}}
}

測試代碼:

st_env_config config;
Iniconfig cof(FILE_NAME, config);cof.addSection("abc");
cof.addValue("abc", "name", "a");
cof.addBoolValue("abc", "vip", true);
cof.addDoubleValue("abc", "length", 175.5);
cof.addLongValue("abc", "weight", 85);cof.setValue("abc", "name", "b");
cof.setBoolValue("abc", "vip", false);
cof.setDoubleValue("abc", "length", 188.8);
cof.setLongValue("abc", "weight", 90);//cof.deleteKey("abc", "name");
//cof.deleteKeys("abc", "vip");
//cof.deleteSection("abc");printf("name = %c\n", cof.getValueC("abc", "name"));
printf("name = %s\n", cof.getValue("abc", "name").c_str());
printf("bool = %d\n", cof.getBoolValue("abc", "vip"));
printf("lenght = %f\n", cof.getDoubleValue("abc", "length"));
printf("weight = %ld\n", cof.getLongValue("abc", "weight"));printf("%s\n", cof.getIniStr().c_str());
cof.saveFile(FILE_NAME);

五、總結(jié)

simpleini庫的基本用法如上面展示的那樣,具體還有一些其他的api,現(xiàn)在還用不到,等用到了,再來補充!

simpleini這個庫應(yīng)該也不算難,無非就是GetValue和SetValue的使用!

ini文件常用來初始化程序,例如存儲一些軟件啟動時初始化的一些基礎(chǔ)數(shù)據(jù),學(xué)習(xí)完這個庫后,日后如果有寫一些小軟件就可以使用ini去初始化了!

http://aloenet.com.cn/news/29317.html

相關(guān)文章:

  • 做黃色網(wǎng)站怎么賺錢瀏覽器下載大全
  • 沒有備案的網(wǎng)站可信嗎徐州seo外包
  • javaee可以做網(wǎng)站么seo收錄排名
  • 網(wǎng)站管理有哪些成人英語培訓(xùn)
  • 新冠病毒的最新動態(tài)廣州seo網(wǎng)站公司
  • 一級造價師注冊查詢系統(tǒng)平臺入口求職seo推薦
  • 自建網(wǎng)站 做自定義導(dǎo)航小廣告網(wǎng)站
  • 網(wǎng)站建設(shè)方案書騰訊云搜資源
  • 電子畢業(yè)設(shè)計代做網(wǎng)站網(wǎng)絡(luò)銷售渠道有哪些
  • 了解網(wǎng)站開發(fā) 后臺流程廣告公司經(jīng)營范圍
  • 東莞做網(wǎng)站企業(yè)銘餐飲營銷案例100例
  • 公司網(wǎng)站建設(shè)會議紀(jì)要網(wǎng)站seo優(yōu)化教程
  • 小學(xué)學(xué)校網(wǎng)站模板免費下載蘇州網(wǎng)站建設(shè)開發(fā)公司
  • 不會編程 做網(wǎng)站chinaz站長素材
  • 免費企業(yè)網(wǎng)站模板下載剛剛中國突然宣布
  • 醫(yī)療整形網(wǎng)站怎么做廣州seo排名優(yōu)化服務(wù)
  • c2c網(wǎng)站頁面設(shè)計特點企業(yè)網(wǎng)站seo點擊軟件
  • 做網(wǎng)站設(shè)計怎么進(jìn)企業(yè)漯河網(wǎng)站推廣公司
  • 網(wǎng)站查詢域名解析ip深圳網(wǎng)絡(luò)推廣培訓(xùn)學(xué)校
  • 華升建設(shè)集團(tuán)公司網(wǎng)站免費建一個自己的網(wǎng)站
  • 最好的網(wǎng)站建設(shè)價格上海b2b網(wǎng)絡(luò)推廣外包
  • 淘寶網(wǎng)站頁面設(shè)計找廣告商的平臺
  • plone cms Wordpress拼多多seo怎么優(yōu)化
  • 做網(wǎng)站買了域名之后關(guān)鍵詞在線優(yōu)化
  • 安卓一鍵制作app軟件長春網(wǎng)站優(yōu)化
  • 自己做的網(wǎng)站在瀏覽器上顯示不安全簡述seo和sem的區(qū)別與聯(lián)系
  • 東莞哪里有做企業(yè)網(wǎng)站的安卓優(yōu)化大師app下載
  • 劉濤給孩子網(wǎng)站做的廣告惠州seo網(wǎng)站排名
  • 淄博網(wǎng)站推廣公司大連seo建站
  • 2021年軍事新聞?wù)緝?yōu)化多少錢