解析一个如下的CONFIG文件

#config.txt
#SHTTPD Web 服务器配置文件示例
#侦听端口
ListenPort = 80
#最大并发访问客户端数目
MaxClient = 8
#Web网页根目录
DocumentRoot = /home/www/
#CGI根目录
CGIRoot = /home/www/cgi-bin/
#默认访问文件名
DefaultFile = index.html
#客户端空闲链接超时时间
TimeOut = 5

  代码

#include <fstream>
#include <string>
#include <map>
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <cctype>
#include <locale> class GetConfigInfo {
public:
bool ReadFile(std::string fileName); private:
std::map<std::string, std::string> configMap;
std::fstream fin_;
}; bool GetConfigInfo::ReadFile(std::string fileName) {
fin_.open(fileName);
if (fin_.bad())
return false;
std::string readLine;
while (getline(fin_, readLine)) //逐行读取,直到结束
{
size_t i = readLine.find("#");
if (i != std::string::npos)
continue;
i = readLine.find_first_of(" \t");
while (i != std::string::npos)
{
readLine.erase(i,1);
i = readLine.find_first_of(" \t");
}
std::cout << readLine << std::endl;
i = readLine.find("=");
if (i == std::string::npos)
continue;
std::string s1 = readLine.substr(0,i);
std::string s2 = readLine.substr(i + 1, std::string::npos);
configMap.insert(std::pair<std::string, std::string>(s1, s2));
} fin_.close();
return true;
} int main()
{
GetConfigInfo gci;
gci.ReadFile("config.txt");
return 0;
}

  

解析config文件 练手代码的更多相关文章

  1. python网页抓取练手代码

    from urllib import request import html.parser class zhuaqu(html.parser.HTMLParser): blogHtml = " ...

  2. JAVA使用SAX解析XML文件

    在我的另一篇文章(http://www.cnblogs.com/anivia/p/5849712.html)中,通过一个例子介绍了使用DOM来解析XML文件,那么本篇文章通过相同的XML文件介绍如何使 ...

  3. java解析properties文件

    在自动化测试过程中,经常会有一些公用的属性要配置,以便后面给脚本使用,我们可以选择xml, excel或者json格式来存贮这些数据,但其实java本身就提供了properties类来处理proper ...

  4. init进程解析rc文件的相关函数分析

    init进程的源码文件位于system/core/init,其中解析rc文件语法的代码放在五个函数中, init_parse_config_file (init_parser.c), read_fil ...

  5. android 生成、pull解析xml文件

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...

  6. 解析Markdown文件生成React组件文档

    前言 最近做的项目使用了微前端框架single-spa. 对于这类微前端框架而言,通常有个utility应用,也就是公共应用,里面是各个子应用之间可以共用的一些公共组件或者方法. 对于一个团队而言,项 ...

  7. webpack练手项目之easySlide(二):代码分割(转)

    在上一篇 webpack练手项目之easySlide(一):初探webpack  中我们一起为大家介绍了webpack的基本用法,使用webpack对前端代码进行模块化打包. 但是乍一看webpack ...

  8. webpack练手项目之easySlide(二):代码分割

    Hello,大家好. 在上一篇 webpack练手项目之easySlide(一):初探webpack  中我们一起为大家介绍了webpack的基本用法,使用webpack对前端代码进行模块化打包. 但 ...

  9. JAVA解析XML文件(DOM,SAX,JDOM,DOM4j附代码实现)

    1.解析XML主要有四种方式 1.DOM方式解析XML(与平台无关,JAVA提供,一次性加载XML文件内容,形成树结构,不适用于大文件) 2.SAX方式解析XML(基于事件驱动,逐条解析,适用于只处理 ...

随机推荐

  1. Visual C++2013 使用技巧

    对 Visual Studio 2013 的 IDE 不熟悉.刚用VS 中的 VC++ IDE 进行编程,一些东西用得少,或以后久了不用,怕又忘了.现在慢慢知道点,记录点,以备以后查阅. 1. 记编译 ...

  2. GIST特征描述符使用(转)

    GIST特征描述符使用 一种场景特征描述 场景特征描述? 通常的特征描述符都是对图片的局部特征进行描述的,以这种思路进行场景描述是不可行的. 比如:对于“大街上有一些行人”这个场景,我们必须通过局部特 ...

  3. sysfs文件系统学习--sysfs

    一.sysfs简介1.sysfs就是利用VFS的接口去读写kobject的层次结构,建立起来的文件系统.其更新与删除是那些xxx_register()/unregister()做的事 情.从sysfs ...

  4. WCF揭秘学习笔记(2):数据表示

    背景知识 WCF提供了一种语言为软件通信建模,称作服务模型.使用更底层的编程架构提供的类可以从这种语言建立的模型中生成可用的通信软件. 在服务模型使用的语言中,负责通信的软件部分称为服务(servic ...

  5. 单线程和多线程处理1W条数据对比代码

    package study.interview; import java.util.ArrayList; import java.util.HashMap; import java.util.Link ...

  6. USACO 2016 January Contest, Gold解题报告

    1.Angry Cows http://www.usaco.org/index.php?page=viewproblem2&cpid=597 dp题+vector数组运用 将从左向右与从右向左 ...

  7. usbip install

    # README for usbip-utils## Copyright (C) 2011 matt mooney <mfm@muteddisk.com>#               2 ...

  8. Bootstrap-Other:可视化布局

    ylbtech-Bootstrap-Other:可视化布局 1.返回顶部 1. 2. 2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部 1. http://www.runoob.co ...

  9. 第5课 Qt Creator工程介绍

    1. QT Creator工程管理(一个工程包含不同类型的文件) (1).pro项目文件 (2).pro.user用户配置描述文件 (3).h头文件 (4).cpp源文件 (5).ui界面描述文件 ( ...

  10. httpclient pool帮助类

    摘自爬虫类  用于频繁请求减少网络消耗 import java.io.IOException; import java.io.InterruptedIOException; import java.i ...