今天分享乙個簡易的c++配置檔案讀取器。大概能讀取的格式如下:
[group1]
arg1=
1arg2=
2.0arg3=d:
/123
#我是一行注釋
[group2]
arg1=woshiniyeye
#pragma once
#include
#include
#include
#include
/*****讀取具有如下形式的配置檔案****************
* [group1]
* arg_float=0.2
* arg_int=1
* arg_string=12345
* #我是一行注釋
* [group2]
* arg_666 = 我是你大哥
* ....
** 每個中括號表示乙個section,不能重複出現
* =號左邊的字段,在每個section中不能重複出現
* setcion、字段、值不能出現空格和中文字元
* # 或 ; 代表注釋行,只能一行一行注釋
******************************************/
class
config
;
#include
"config.h"
#include
#include
#include
#include
#define printf(str,...) printf(str,__va_args__);printf("\n");
void
config_expfunc
(const
char
* expr,
const
char
* func,
const
char
* file,
int line)
#define config_assert( expr ) do while(0)
std::string config::
escapestring
(const std::string& src)
void config::
getgroup
(std::map<:string std::string>
& param,std::string name)
}template
<
typename t>
t config::
getparam
(const std::map<:string std::string>
& params,
const std::string param_name, t init_val)
else
return init_val;
}template
<
>
std::string config::getparam<:string>
(const std::map<:string std::string>
& params,
const std::string param_name, std::string init_val)
else
return init_val;
}void config::
readconfig
(const
char
* filename)
else
break
;default
://讀取每個分組裡的節點內容
const size_t separator_index = line.
find
('=');
config_assert
(separator_index < line.
size()
);config_assert
(!m_params.
empty()
);if(separator_index != std::string::npos)
break;}
}}catch
(std::exception& msg)
catch(.
..)}
printf
("讀取配置檔案節點資料");
//section
std::map<:string std::string> param;
getgroup
(param,
"group1");
arg_float =
getparam
(param,
"arg_float"
,0.2);
arg_int =
getparam
(param,
"arg_int",1
);arg_string = getparam<:string>
(param,
"arg_string"
,"12345");
getgroup
(param,
"group2");
arg_666 = getparam<:string>
(param,
"arg_666"
,"我是你大哥");
printf
("配置檔案讀取成功");
C 讀取配置檔案
1 首先引入標頭檔案 include 2 獲取應用程式的當前路徑 char buf 1000 getcurrentdirectory 1000,buf 得到當前工作路徑3 獲取配置檔案的路徑 char path 1024 definesysconfig config.ini sprintf path...
C 讀取配置檔案
1 利用系統介面,讀取ini配置檔案,詳情可查詢msdn。這裡做簡單宣告 宣告讀ini檔案的api函式 dllimport kernel32 private static extern int getprivateprofilestring string section,string key,str...
C讀取配置檔案
ifndef cfg op h define cfg op h ifdef cplusplus extern c endif endif define crt secure no warnings include include include define maxline 2048 獲取配置項 i...