C++读取配置文件

摘要:
在牛人的指导下,它与之前的版本有了很大的变化。逐行读取配置文件,然后逐行解析它~读取一次后,将键值对存储在映射中,然后从映射中检索它们。减少文件读取次数的主要代码如下:/**readconfigfile,add<键、值>intomap.*@paramfilepath(in)linetext*@paramreturn*-1:错误,无效行*0:成功*

在牛人的指导下,和前一个版本有了较大改变。

逐行读取配置文件,然后逐行解析~

读取一次之后,将键值对存入map,之后都从map中去取,减少读取文件次数

主要代码如下:

/**
* 
* read config file, add <key,value> into map.
* @param filepath (in)line text
* @param return  
*        -1:error,invalid line
*        0:success
*           
*/
int INIReader::readFile(const wstring &filename) {
    std::string strFilename(filename.begin(), filename.end());
    wifstream infile(strFilename.c_str());
    wstring buffer;
    while (getline(infile, buffer)) {
        parseContentLine(buffer);
    }

    return 0;
}

/**
* 
* handle single line text,then add <key,value> into map.
* @param filepath (in)line text
* @param return  
*        -1:error,invalid line
*        0:success
*           
*/
int INIReader::parseContentLine(wstring &contentLine) {
    contentLine = trim(contentLine);
    if (contentLine.size() < 1) {
        return 0;   // blank line
    }

    if (contentLine.substr(0, 1) == ANNOTATION_SYMBOL1 
        || contentLine.substr(0, 1) == ANNOTATION_SYMBOL2) {
            return 0;   // 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 <<"	" << value << std::endl;
    return 0;
}

点此下载整个代码工程

免责声明:文章转载自《C++读取配置文件》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Revit 二次开发 族的练习C++各大有名库的介绍——网络通信下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Qt horizontal line和vertical line的使用方法及在 QGridLayout 中的应用

1.horizontal line 和vertical line 的使用horizontal line 和vertical line 实际上是由QFame类,设置相应的参数来实现的;而不是由单独的类来实现的,例如(QHorizontalLine/QVerticalLine); 1.1.控件拖拽  1.2.代码实现 1 QFrame *line =...

创建odoo数据库时出现错误原因

安装完odoo 8.0后创建数据库时出现如下错误信息: Odoo Odoo Server Error Traceback (most recent call last): File "D:Odoosourceopenerphttp.py", line 537, in _handle_exception return super(JsonRe...

转:Oracle 10g批量绑定forall bulk collect

批量绑定可以通过减少在PL/SQL和SQL引擎之间的上下文切换(context switches )以此提高性能。批量绑定(Bulk binds)主要包括:(1) Input collections, use the FORALL statement,用来改善DML(INSERT、UPDATE和DELETE)操作的性能。(2) Output collect...

iptables规则的查看、添加、删除和修改

  http://ns.35.com/?p=211 1、查看iptables -nvL –line-number -L 查看当前表的所有规则,默认查看的是filter表,如果要查看NAT表,可以加上-t NAT参数 -n 不对ip地址进行反查,加上这个参数显示速度会快很多 -v 输出详细信息,包含通过该规则的数据包数量,总字节数及相应的网络接口 –line...

TypeError: 'NoneType' object is not iterable 一次错误场景

TypeError: 'NoneType' object is not iterable 源码 def get_url(lines): urls=[] for line in lines: if 'img' in urls: url=line.split('src='http://t.zou...

LeetCode刷题中遇到的bug

以下bug都是本人用C刷题时遇到的 bug1:函数为被明确定义 solution.c: In function ‘isSubtree’ Line 13: Char 12: warning: implicit declaration of function ‘isSameTree’; did you mean ‘isSubtree’? [-Wimplicit...