有关C++java写配置文件件的问题

在 SegmentFault,学习技能、解决问题
每个月,我们帮助 1000 万的开发者解决各种各样的技术问题。并助力他们在技术能力、职业生涯、影响力上获得提升。
问题对人有帮助,内容完整,我也想知道答案
问题没有实际价值,缺少关键内容,没有改进余地
问题背景:现在有一个XML配置文件,里面有大量的数据来初始化本地程序的变量,问题:怎么样读取并处理这个巨大的XML配置文件比较方便?是直接用XML解析器通过一个个节点地遍历整个DOM树来处理,并在每一个节点对应一条初始化语句吗?感觉这样初始化程序量比较大,有没有什么通用的方法或者方便的工具?谢谢!
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
可以一层层解析。使用xml spy看起来比较有层次
同步到新浪微博
分享到微博?
关闭理由:
删除理由:
忽略理由:
推广(招聘、广告、SEO 等)方面的内容
与已有问题重复(请编辑该提问指向已有相同问题)
答非所问,不符合答题要求
宜作评论而非答案
带有人身攻击、辱骂、仇恨等违反条款的内容
无法获得确切结果的问题
非开发直接相关的问题
非技术提问的讨论型问题
其他原因(请补充说明)
我要该,理由是:
在 SegmentFault,学习技能、解决问题
每个月,我们帮助 1000 万的开发者解决各种各样的技术问题。并助力他们在技术能力、职业生涯、影响力上获得提升。当前位置:
纯c读写ini配置文件
猜你喜欢的文章
纯c读写ini配置文件
来源:网络整理&&&&&时间: 0:26:53&&&&&关键词:
关于网友提出的“ 纯c读写ini配置文件”问题疑问,本网通过在网上对“ 纯c读写ini配置文件”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题: 纯c读写ini配置文件
描述:本帖最后由 taoyuanmin 于
11:54:20 编辑c语言ini
纯c读写ini配置文件
用c/c++读写ini配置文件有不少第三方的开源库,如iniparser、libini、rwini、UltraLightINIParser等,但都不理想,往往代码较大、功能较弱、接口使用不方便。可能在大小写处理、前后空格、各种注释、跨平台换行符支持、带引号字符串处理、无section操作、原格式保持等方面存在问题。
现将本人精心制作的ini读写程序源码奉献给大家,纯c编写,简洁好用。支持windows和linux。
主要特点:
&&1、支持;和#注释符号,支持行尾注释。
&&2、支持带引号'或"成对匹配的字符串,提取时自动去引号。引号中可带其它引号或;#注释符。
&&3、支持无section(无[])或空section(名称为空)。
&&4、支持10、16、8进制数,0x开头为16进制数,0开头为8进制。
&&5、支持section、key或=号前后带空格。
&&6、支持\n、\r、\r\n或\n\r换行格式。
&&7、不区分section、key大小写,但写入时以新串为准,并保持其大小写。
&&8、新增数据时,若section存在则在该节最后一个有效数据后添加,否则在文件尾部添加。
&&9、支持指定key所在整行删除,即删除该键值,包括注释。
&&10、可自动跳过格式错误行,修改时仍然保留。
&&11、修改时保留原注释:包括整行注释、行尾注释(包括前面空格)。
&&12、修改时保留原空行。以上三点主要是尽量保留原格式。
不足之处:
&&1、不支持单key多value(逗号分割),只能一次性提取后自行处理。
&&2、不支持同名重复section和key。(重复section可视为错误,重复key则可能造成分歧)
&&3、不能提取所有section或key名称。
使用只需两个文件inirw.h、inirw.c,另有测试程序和工程文件,支持windows和linux。
下载地址:
http://download.csdn.net/detail/taoyuanmin/9179573
以上介绍了“ 纯c读写ini配置文件”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/4319424.html
上一篇: 下一篇:求高手帮写个c++读写配置文件类的实现代码
[问题点数:40分,结帖人andywangcn]
本版专家分:0
结帖率 96.97%
CSDN今日推荐
本版专家分:4827
本版专家分:4198
本版专家分:218
本版专家分:0
本版专家分:0
匿名用户不能发表回复!
其他相关推荐老外写的一段代码,在Server中编写这个类读取配置文件比较实用
#pragma once
#include &string&
#include &map&
#include &iostream&
#include &fstream&
#include &sstream&
class Config {
protected:
std::string m_D
std::string m_C
std::map&std::string,std::string& m_C
typedef std::map&std::string,std::string&::
typedef std::map&std::string,std::string&::const_
Config( std::string filename,std::string delimiter = "=",std::string comment = "#" );
template&class T& T Read( const std::string& in_key ) const;
template&class T& T Read( const std::string& in_key, const T& in_value ) const;
template&class T& bool ReadInto( T& out_var, const std::string& in_key ) const;
template&class T&
bool ReadInto( T& out_var, const std::string& in_key, const T& in_value ) const;
bool FileExist(std::string filename);
void ReadFile(std::string filename,std::string delimiter = "=",std::string comment = "#" );
bool KeyExists( const std::string& in_key ) const;
template&class T& void Add( const std::string& in_key, const T& in_value );
void Remove( const std::string& in_key );
std::string GetDelimiter() const { return m_D }
std::string GetComment() const { return m_C }
std::string SetDelimiter( const std::string& in_s )
{ std::string old = m_D
m_Delimiter = in_s;
std::string SetComment( const std::string& in_s )
{ std::string old = m_C
m_Comment =
friend std::ostream& operator&&( std::ostream& os, const Config& cf );
friend std::istream& operator&&( std::istream& is, Config& cf );
protected:
template&class T& static std::string T_as_string( const T& t );
template&class T& static T string_as_T( const std::string& s );
static void Trim( std::string& inout_s );
struct File_not_found {
File_not_found( const std::string& filename_ = std::string() )
: filename(filename_) {} };
struct Key_not_found {
Key_not_found( const std::string& key_ = std::string() )
: key(key_) {} };
template&class T&
std::string Config::T_as_string( const T& t )
return ost.str();
template&class T&
T Config::string_as_T( const std::string& s )
std::istringstream ist(s);
template&&
inline std::string Config::string_as_T&std::string&( const std::string& s )
template&&
inline bool Config::string_as_T&bool&( const std::string& s )
bool b = true;
std::string sup =
for( std::string::iterator p = sup.begin(); p != sup.end(); ++p )
*p = toupper(*p);
if( sup==std::string("FALSE") || sup==std::string("F") ||
sup==std::string("NO") || sup==std::string("N") ||
sup==std::string("0") || sup==std::string("NONE") )
b = false;
template&class T&
T Config::Read( const std::string& key ) const
mapci p = m_Contents.find(key);
if( p == m_Contents.end() ) throw Key_not_found(key);
return string_as_T&T&( p-&second );
template&class T&
T Config::Read( const std::string& key, const T& value ) const
mapci p = m_Contents.find(key);
if( p == m_Contents.end() ) return
return string_as_T&T&( p-&second );
template&class T&
bool Config::ReadInto( T& var, const std::string& key ) const
mapci p = m_Contents.find(key);
bool found = ( p != m_Contents.end() );
if( found ) var = string_as_T&T&( p-&second );
template&class T&
bool Config::ReadInto( T& var, const std::string& key, const T& value ) const
mapci p = m_Contents.find(key);
bool found = ( p != m_Contents.end() );
if( found )
var = string_as_T&T&( p-&second );
template&class T&
void Config::Add( const std::string& in_key, const T& value )
std::string v = T_as_string( value );
std::string key=in_
trim(key);
m_Contents[key] =
#include "Config.h"
using namespace
Config::Config( string filename, string delimiter,
string comment )
: m_Delimiter(delimiter), m_Comment(comment)
std::ifstream in( filename.c_str() );
if( !in ) throw File_not_found( filename );
in && (*this);
Config::Config()
: m_Delimiter( string(1,'=') ), m_Comment( string(1,'#') )
bool Config::KeyExists( const string& key ) const
mapci p = m_Contents.find( key );
return ( p != m_Contents.end() );
void Config::Trim( string& inout_s )
static const char whitespace[] = " \n\t\v\r\f";
inout_s.erase( 0, inout_s.find_first_not_of(whitespace) );
inout_s.erase( inout_s.find_last_not_of(whitespace) + 1U );
std::ostream& operator&&( std::ostream& os, const Config& cf )
for( Config::mapci p = cf.m_Contents.begin();
p != cf.m_Contents.end();
os && p-&first && " " && cf.m_Delimiter && " ";
os && p-&second && std::
void Config::Remove( const string& key )
m_Contents.erase( m_Contents.find( key ) );
std::istream& operator&&( std::istream& is, Config& cf )
typedef string::size_
const string& delim
const string& comm
const pos skip = delim.length();
string nextline = "";
while( is || nextline.length() & 0 )
if( nextline.length() & 0 )
nextline = "";
std::getline( is, line );
line = line.substr( 0, line.find(comm) );
pos delimPos = line.find( delim );
if( delimPos & string::npos )
string key = line.substr( 0, delimPos );
line.replace( 0, delimPos+skip, "" );
bool terminate = false;
while( !terminate && is )
std::getline( is, nextline );
terminate = true;
string nlcopy =
Config::Trim(nlcopy);
if( nlcopy == "" ) continue;
nextline = nextline.substr( 0, nextline.find(comm) );
if( nextline.find(delim) != string::npos )
Config::Trim(nlcopy);
if( nlcopy != "" ) line += "\n";
terminate = false;
Config::Trim(key);
Config::Trim(line);
cf.m_Contents[key] =
bool Config::FileExist(std::string filename)
bool exist= false;
std::ifstream in( filename.c_str() );
exist = true;
void Config::ReadFile( string filename, string delimiter,
string comment )
m_Delimiter =
m_Comment =
std::ifstream in( filename.c_str() );
if( !in ) throw File_not_found( filename );
in && (*this);
#include "Config.h"
int main()
std::string ipA
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:"&&port&&std::
std::cout&&"ipAddress:"&&ipAddress&&std::
std::cout&&"username:"&&username&&std::
std::cout&&"password:"&&password&&std::
config.txt的文件内容: ipAddress=10.10.90.125 port=3001 username=mark password=2d2df5a 编译运行输出: port:3001 ipAddress:10.10.90.125 username:mark password:2d2df5a 这个类还有很多其他的方法,可以调用试试。
原文地址:
C++读取配置文件的几种方法
C++读取配置文件
C++读取、写入配置文件
c/c++写的比较好的读写配置文件的函数或者类
C++针对ini配置文件读写大全
配置文件读取的简要C/C++代码
c++配置文件ini---GetPrivateProfileString(...)\WritePrivateProfileString(...)
C++操作.ini配置文件的API
C++实现解析INI配置文件
c++ 配置文件操作类
没有更多推荐了,在牛人的指导下,和有了较大改变。
逐行读取配置文件,然后逐行解析~
读取一次之后,将键值对存入map,之后都从map中去取,减少读取文件次数
主要代码如下:
* read config file, add &key,value& into map.
* @param filepath (in)line text
* @param return
-1:error,invalid line
int INIReader::readFile(const wstring &filename) {
std::string strFilename(filename.begin(), filename.end());
wifstream infile(strFilename.c_str());
while (getline(infile, buffer)) {
parseContentLine(buffer);
* handle single line text,then add &key,value& into map.
* @param filepath (in)line text
* @param return
-1:error,invalid line
int INIReader::parseContentLine(wstring &contentLine) {
contentLine = trim(contentLine);
if (contentLine.size() & 1) {
// blank line
if (contentLine.substr(0, 1) == ANNOTATION_SYMBOL1
|| contentLine.substr(0, 1) == ANNOTATION_SYMBOL2) {
// comment
wstring::size_type equalPos = contentLine.find_first_of(L"=");
wstring::size_type startPos = 0;
wstring::size_type endPos = contentLine.size() - 1;
if (equalPos &= startPos || equalPos & endPos) {
return -1; // invalid line
wstring key = rtrim(contentLine.substr(startPos, equalPos ));
wstring value = ltrim(contentLine.substr(equalPos + 1, endPos));
paramMap.insert(std::make_pair(key, value));
//std::wcout &&key &&"\t" && value && std::
点此整个代码工程
阅读(...) 评论()

我要回帖

更多关于 页面配置文件问题 的文章

 

随机推荐