C 讀取配置檔案的封裝類config

2021-07-01 20:01:53 字數 3560 閱讀 6587

c++中讀取配置檔案的類封裝,很好用,直接可以用

config.h

#pragma once

#include #include #include #include #include /*

* \brief generic configuration class**/

class config

std::string getcomment() const

std::string setdelimiter( const std::string& in_s )

std::string setcomment( const std::string& in_s )

// write or read configuration

friend std::ostream& operator<<( std::ostream& os, const config& cf );

friend std::istream& operator>>( std::istream& is, config& cf );

protected:

templatestatic std::string t_as_string( const t& t );

templatestatic t string_as_t( const std::string& s );

static void trim( std::string& inout_s );

// exception types

public:

struct file_not_found };

struct key_not_found };

};/* static */

templatestd::string config::t_as_string( const t& t )

/* static */

templatet config::string_as_t( const std::string& s )

/* static */

template<>

inline std::string config::string_as_t( const std::string& s )

/* static */

template<>

inline bool config::string_as_t( const std::string& s )

templatet config::read( const std::string& key ) const

templatet config::read( const std::string& key, const t& value ) const

templatebool config::readinto( t& var, const std::string& key ) const

templatebool config::readinto( t& var, const std::string& key, const t& value ) const

templatevoid config::add( const std::string& in_key, const t& value )

config.cpp

// config.cpp

#include "config.h"

using namespace std;

config::config( string filename, string delimiter,

string comment )

: m_delimiter(delimiter), m_comment(comment)

config::config()

: m_delimiter( string(1,'=') ), m_comment( string(1,'#') )

bool config::keyexists( const string& key ) const

/* static */

void config::trim( string& inout_s )

std::ostream& operator<<( std::ostream& os, const config& cf )

return os;

}void config::remove( const string& key )

std::istream& operator>>( std::istream& is, config& cf )

else

// ignore comments

line = line.substr( 0, line.find(comm) );

// parse the line if it contains a delimiter

pos delimpos = line.find( delim );

if( delimpos < string::npos )

// store key and value

config::trim(key);

config::trim(line);

cf.m_contents[key] = line; // overwrites if key is repeated

} }return is;

}bool config::fileexist(std::string filename)

void config::readfile( string filename, string delimiter,

string comment )

測試#include "config.h"

int main()

{ int port;

std::string ipaddress;

std::string username;

std::string password;

const char configfile= "config.txt";

config configsettings(configfile);

port = configsettings.read("port", 0);

ipaddress = configsettings.read("ipaddress", ipaddress);

username = configsettings.read("username", username);

password = configsettings.read("password", password);

std::cout<<"port:"ipaddress=10.10.90.125 

port=3001 

username=mark 

password=2d2df5a 

編譯執行輸出: 

port:3001 

ipaddress:10.10.90.125 

username:mark 

password:2d2df5a 

這個類還有很多其他的方法,可以呼叫試試。

Linux 通用配置檔案conf讀取

linux下通用配置檔案讀取 include include include include include define keyvallen 100 刪除左邊的空格 char l trim char szoutput,const char szinput return strcpy szoutpu...

MFC 讀取配置檔案(封裝)

目前只封裝了string型別的讀和寫配置檔案 利用了 windows api 現有的函式,很簡單 writeprivateprofilestringw in opt lpcwstr lpkeyname,in opt lpcwstr lpstring,in opt lpcwstr lpfilename...

怎麼修改conf配置檔案

文字模式下用vi,按a或i鍵進行編輯,編輯完後按esc,然後輸入 wq 儲存,如不想儲存則輸入 q 執行man vi有更詳細的說明 用vi如上所示,不過我習慣的儲存方法是shift 2次z emacs就直接可以編輯,完了後按f10,有選單選擇的top 在 linux 底下最常使用的文書編輯器為 vi...