C++讀取配置文件的示例代碼
代碼地址
https://github.com/gongluck/Code-snippet/tree/master/cpp/config
需求
開(kāi)發(fā)中,讀取配置文件信息必不可少。Windows平臺(tái)有現(xiàn)成的API可用,也很方便。但是一旦項(xiàng)目遷移到Linux平臺(tái)下,原先在Windows平臺(tái)下的代碼就全部作廢。所以,實(shí)現(xiàn)一套跨平臺(tái)的配置文件讀取功能代碼可以節(jié)省不少的勞動(dòng)力。
實(shí)現(xiàn)
依賴(lài)于boost的ini_parser,可以實(shí)現(xiàn)跨平臺(tái)讀取ini格式的配置文件。
// config.h
/*
* @Author: gongluck
* @Date: 2020-03-23 15:11:50
* @Last Modified by: gongluck
* @Last Modified time: 2020-03-23 15:17:58
*/
// Profile read, dependent on boost
#pragma once
#include <iostream>
#include <vector>
#include <boost/property_tree/ptree.hpp>
namespace gconf
{
class config
{
public:
int open(const char *configfile);
template <typename T>
int read(const char *session, const char *key, T &value, const char *configfile = nullptr)
{
if (configfile != nullptr && open(configfile) != 0)
{
return -1;
}
try
{
auto lvbtItems = lvptProperties_.get_child(session);
value = lvbtItems.get<T>(key);
}
catch (std::exception &e)
{
std::cerr << __FILE__ << " : " << __LINE__ << " : " << e.what() << std::endl;
return -1;
}
return 0;
}
int readall(const char *session,
std::vector<std::pair<std::string, std::string>> &results,
const char *configfile = nullptr);
private:
boost::property_tree::ptree lvptProperties_;
};
} // namespace gconf
// config.cpp
/*
* @Author: gongluck
* @Date: 2020-03-23 15:13:13
* @Last Modified by: gongluck
* @Last Modified time: 2020-03-23 15:17:56
*/
#include "config.h"
#include <boost/property_tree/ini_parser.hpp>
namespace gconf
{
int config::open(const char *configfile)
{
if (configfile == nullptr)
{
return -1;
}
try
{
boost::property_tree::ini_parser::read_ini(configfile, lvptProperties_);
}
catch (std::exception &e)
{
std::cerr << __FILE__ << " : " << __LINE__ << " : " << e.what() << std::endl;
return -1;
}
return 0;
}
int config::readall(const char *session,
std::vector<std::pair<std::string, std::string>> &results,
const char *configfile /*= nullptr*/)
{
if (configfile != nullptr && open(configfile) != 0)
{
std::cerr << __FILE__ << " : " << __LINE__ << " : "
<< " can not open " << configfile << std::endl;
return -1;
}
try
{
auto lvbtItems = lvptProperties_.get_child(session);
for (const auto &i : lvbtItems)
{
results.push_back(std::make_pair(i.first.data(), i.second.data()));
}
}
catch (std::exception &e)
{
std::cerr << __FILE__ << " : " << __LINE__ << " : " << e.what() << std::endl;
return -1;
}
return 0;
}
} // namespace gconf
// testcode
#include <iostream>
#include "../config/config.h"
#define CHECKRET(ret)\
if(ret != 0)\
{\
std::cin.get();\
return ret;\
}
int main()
{
gconf::config conf;
auto ret = conf.open("./config.ini");
CHECKRET(ret);
int file = 0;
ret = conf.read<int>("log", "file", file);
CHECKRET(ret);
std::vector<std::pair<std::string, std::string>>kvs;
ret = conf.readall("log", kvs);
CHECKRET(ret);
return 0;
}
以上就是C++讀取配置文件的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于C++讀取配置文件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- 最新VScode C/C++ 環(huán)境配置的詳細(xì)教程
- vs code 配置c/c++環(huán)境的詳細(xì)教程(推薦)
- Linux配置C++11編譯環(huán)境的方法
- vscode 配置 C/C++編譯環(huán)境(完整教程)
- VS Code C/C++環(huán)境配置教程(無(wú)法打開(kāi)源文件“xxxxxx.h”或者檢測(cè)到 #include 錯(cuò)誤,請(qǐng)更新includePath)(POSIX API)
- C++讀寫(xiě)ini配置文件實(shí)現(xiàn)過(guò)程詳解
- Visual Studio Code配置C/C++開(kāi)發(fā)環(huán)境的教程圖解
- Visual Studio Code 配置C、C++環(huán)境/編譯并運(yùn)行的流程分析
- Ubuntu 20.04 下安裝配置 VScode 的 C/C++ 開(kāi)發(fā)環(huán)境(圖文教程)
- Windows配置VSCode+CMake+Ninja+Boost.Test的C++開(kāi)發(fā)環(huán)境(教程詳解)
- C++讀寫(xiě)配置項(xiàng)的基本操作
相關(guān)文章
QT實(shí)現(xiàn)QML側(cè)邊導(dǎo)航欄的最簡(jiǎn)方法
本文主要介紹了QT實(shí)現(xiàn)QML側(cè)邊導(dǎo)航欄的最簡(jiǎn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
詳解C++的JSON靜態(tài)鏈接庫(kù)JsonCpp的使用方法
這篇文章主要介紹了C++的JSON靜態(tài)鏈接庫(kù)JsonCpp的使用方法,演示了使用JsonCpp生成和解析JSON的方法,以及C++通過(guò)JSON方式的socket通信示例,需要的朋友可以參考下2016-03-03
C語(yǔ)言實(shí)現(xiàn)三子棋游戲的示例代碼
今天我們將會(huì)用C語(yǔ)言實(shí)現(xiàn)三子棋。所謂三子棋,就是三行三列的棋盤(pán),玩家可以和電腦下棋,率先連成三個(gè)的獲勝。話不多說(shuō),我們開(kāi)始吧2022-10-10
C++?OpenCV實(shí)現(xiàn)白平衡之完美反射算法
完美反射算法是白平衡各種算法中較常見(jiàn)的一種,比灰度世界算法更優(yōu)。本文將利用C++和OpenCV實(shí)現(xiàn)白平衡中的完美反射算法,需要的可以參考一下2022-05-05
用VC++6.0實(shí)現(xiàn)石頭剪刀布游戲的程序
最先看到這個(gè)游戲代碼是python版的,后來(lái)看到有小伙伴用VC++重寫(xiě)了一遍,運(yùn)行之后發(fā)現(xiàn)有些小bug,便嘗試這修復(fù)了一下,并增加了些小功能,這里分享給大家。2015-03-03
怎么通過(guò)C語(yǔ)言自動(dòng)生成MAC地址
以下是對(duì)使用C語(yǔ)言自動(dòng)生成MAC地址的實(shí)現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過(guò)來(lái)參考下2013-09-09

