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

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

读取一次之后,将键值对存入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 ;
} /**
*
* 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() < ) {
return ; // blank line
} if (contentLine.substr(, ) == ANNOTATION_SYMBOL1
|| contentLine.substr(, ) == ANNOTATION_SYMBOL2) {
return ; // comment
} wstring::size_type equalPos = contentLine.find_first_of(L"=");
wstring::size_type startPos = ;
wstring::size_type endPos = contentLine.size() - ; if (equalPos <= startPos || equalPos > endPos) {
return -; // invalid line
} wstring key = rtrim(contentLine.substr(startPos, equalPos ));
wstring value = ltrim(contentLine.substr(equalPos + , endPos)); paramMap.insert(std::make_pair(key, value)); //std::wcout <<key <<"\t" << value << std::endl;
return ;
}

点此下载整个代码工程

C++读取配置文件的更多相关文章

  1. 【无私分享:ASP.NET CORE 项目实战(第八章)】读取配置文件(二) 读取自定义配置文件

    目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 我们在 读取配置文件(一) appsettings.json 中介绍了,如何读取appsettings.json. 但随之产生 ...

  2. 解决IntelliJ IDEA无法读取配置文件的问题

    解决IntelliJ IDEA无法读取配置文件的问题 最近在学Mybatis,按照视频的讲解在项目的某个包里建立配置文件,然后读取配置文件,但是一直提示异常. 读取配置文件的为官方代码: String ...

  3. java-工具类-读取配置文件

    java读取配置文件,当发现文件被修改后则重新加载 package com.zg.config; import java.io.File; import java.io.FileInputStream ...

  4. java 4种方式读取配置文件 + 修改配置文件

    版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[-] 方式一采用ServletContext读取读取配置文件的realpath然后通过文件流读取出来 方式二采用ResourceB ...

  5. 在IIS Express中调试时无法读取配置文件 错误

    在IIS Express中调试代码时,如果出现"无法读取配置文件"的问题(如图),这种情况是IIS Express的"applicationhost.config&quo ...

  6. ASP.NET Core开发-读取配置文件Configuration

    ASP.NET Core 是如何读取配置文件,今天我们来学习. ASP.NET Core的配置系统已经和之前版本的ASP.NET有所不同了,之前是依赖于System.Configuration和XML ...

  7. Java 利用 ByteArrayOutputStream 和 ByteArrayInputStream 避免重复读取配置文件

    最近参与了github上的一个开源项目 Mycat,是一个mysql的分库分表的中间件.发现其中读取配置文件的代码,存在频繁多次重复打开,读取,关闭的问题,代码写的很初级,稍微看过一些框架源码的人,是 ...

  8. Python+Selenium进行UI自动化测试项目中,常用的小技巧2:读取配置文件(configparser,.ini文件)

    在自动化测试项目中,可能会碰到一些经常使用的但 很少变化的配置信息,下面就来介绍使用configparser来读取配置信息config.ini 读取的信息(config.ini)如下: [config ...

  9. win7 web开发遇到的问题-由于权限不足而无法读取配置文件,无法访问请求的页面

    错误一: HTTP Error 500.19 - Internal Server Error配置错误: 不能在此路径中使用此配置节.如果在父级别上锁定了该节,便会出现这种情况.锁定是默认设置的 (ov ...

  10. python中读取配置文件ConfigParser

    在程序中使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在python里更是如此,在官方发布的库中就包含有做这件事情的库,那就是ConfigParser,这里简单的做一些介 ...

随机推荐

  1. Openwrt 远程调试

    此文已由作者吴志勐授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 本文以自己的程序WFD为例: 1,为路由器固件刷上gdbserver 在宿主端,使用make menucon ...

  2. Open vSwitch 2.9.2 创建 RPM 安装包

    1.安装依赖环境 yum install gcc make python-devel openssl-devel graphviz autoconf automake rpm-build redhat ...

  3. 920. Number of Music Playlists

    Your music player contains N different songs and she wants to listen to L (not necessarily different ...

  4. PHPhotos

    PHPhotoLibrary: @abstract A PHPhotoLibrary provides access to the metadata and image data for the ph ...

  5. jQuery Validation Plugin

    使用方式很简单,简单测试代码如下: <html> <head> <script type="text/javascript" src="./ ...

  6. POJ 2242

    #include <iostream> #include <cmath> #include <iomanip> using namespace std; #defi ...

  7. POJ 1160

    #include <iostream> #define MAXN 305 #define inf 123456789 using namespace std; int _m[MAXN][M ...

  8. POJ 1051

    #include <iostream> #include <algorithm> #include <string> #define MAXN 27 using n ...

  9. (转) rabbitmq应用场景

    原文:http://blog.csdn.net/wangpengblog/article/details/76405598

  10. wp-postviews使用方法

    1.安装 2.头部引用<?php wp_head(); ?> 3.文章调用:<?php if(function_exists('the_views')) { the_views(); ...