C++读取配置文件
来源:互联网 发布:dorado js怎么调试 编辑:程序博客网 时间:2024/05/16 07:12
C++读取配置文件
头文件:get_config.h
/****************************************************************************
* 作者: jasitzhang(张涛)
* 日期: 2011-10-2
* 目的: 读取配置文件的信息,以map的形式存入
* 要求: 配置文件的格式,以#作为行注释,配置的形式是key = value,中间可有空格,也可没有空格
*****************************************************************************/
#ifndef _GET_CONFIG_H_
#define _GET_CONFIG_H_
#include <string>
#include <map>
using
namespace
std;
#define COMMENT_CHAR '#'
bool
ReadConfig(
const
string & filename, map<string, string> & m);
void
PrintConfig(
const
map<string, string> & m);
#endif
源文件:get_config.cpp
#include "get_config.h"#include <fstream>#include <iostream>using namespace std;bool IsSpace(char c){ if (' ' == c || '\t' == c) return true; return false;}bool IsCommentChar(char c){ switch(c) { case COMMENT_CHAR: return true; default: return false; }}void Trim(string & str){ if (str.empty()) { return; } int i, start_pos, end_pos; for (i = 0; i < str.size(); ++i) { if (!IsSpace(str[i])) { break; } } if (i == str.size()) { // 全部是空白字符串 str = ""; return; } start_pos = i; for (i = str.size() - 1; i >= 0; --i) { if (!IsSpace(str[i])) { break; } } end_pos = i; str = str.substr(start_pos, end_pos - start_pos + 1);}bool AnalyseLine(const string & line, string & key, string & value){ if (line.empty()) return false; int start_pos = 0, end_pos = line.size() - 1, pos; if ((pos = line.find(COMMENT_CHAR)) != -1) { if (0 == pos) { // 行的第一个字符就是注释字符 return false; } end_pos = pos - 1; } string new_line = line.substr(start_pos, start_pos + 1 - end_pos); // 预处理,删除注释部分 if ((pos = new_line.find('=')) == -1) return false; // 没有=号 key = new_line.substr(0, pos); value = new_line.substr(pos + 1, end_pos + 1- (pos + 1)); Trim(key); if (key.empty()) { return false; } Trim(value); return true;}bool ReadConfig(const string & filename, map<string, string> & m){ m.clear(); ifstream infile(filename.c_str()); if (!infile) { cout << "file open error" << endl; return false; } string line, key, value; while (getline(infile, line)) { if (AnalyseLine(line, key, value)) { m[key] = value; } } infile.close(); return true;}void PrintConfig(const map<string, string> & m){ map<string, string>::const_iterator mite = m.begin(); for (; mite != m.end(); ++mite) { cout << mite->first << "=" << mite->second << endl; }}
测试数据:test.cfg
#added by jasit
key1 = value1
key2 = value2
key3 = value3 + value3
sdf
测试源程序:test.cpp
#include "get_config.h"
int
main()
{
map<string, string> m;
ReadConfig(
"test2.cfg"
, m);
PrintConfig(m);
return
0;
}
makefile:
BIN := test CFLAGS := -g -static -WallCC := g++ SRCFILE := $(wildcard *.cpp) OBJFILE := $(patsubst %.cpp,%.o,$(SRCFILE))$(BIN): $(OBJFILE) $(CC) $(CFLAGS) -o $(BIN) $(OBJFILE) %.o:%.cpp $(CC) $(CFLAGS) -c $< -o $@clean : rm -rf $(OBJFILE) ${BIN}
- C 读取配置文件函数
- C读取配置文件
- C语言读取配置文件
- linux c读取配置文件
- C语言读取配置文件
- C语言读取配置文件
- linux c 读取配置文件
- C语言读取配置文件
- 读取配置文件源代码[linux c]
- C 添加, 读取配置文件 函数
- 用C读取INI配置文件
- 用C读取INI配置文件
- linux读取配置文件(C语言版)
- linux读取配置文件(C语言版)
- C语言读取配置文件例子
- linux读取配置文件(C语言版)
- 用C读取INI配置文件
- linux读取配置文件(C语言版)
- [VC++]配置文件读取
- vc如何在MSflexgrid表格的最后一行增加一行数据
- 带命令行参数的main函数详解[转载]
- 用实例给新手讲解RSA加密算法
- flash 3d示例
- C++读取配置文件
- java项目生成"可运行jar"或"exe"的配置文件注意点
- cas+tomcat+liferay6.1整合
- JUnit学习(1)
- DefineDosDevice函数控制虚拟驱动器
- FS_S5PC100平台上Linux Camera驱动开发详解
- Mac 基本命令
- VNC远程桌面到linux,提示connection refused(10061)解决办法
- android中ToggleButton的使用