c++读取配置文件

最近做的一个C++项目需要做一个配置文件,然后从配置文件读取信息,随便封装了一下,方便使用。下面是代码和使用说明。

operate_config.h:

/****************************************************************************
*   作者:  符星
*   日期:  2013-4-14
*   目的:  读取配置文件的信息,以string的形式返回
*   要求:  配置文件的格式,以#作为行注释,配置的形式是key = value,中间可有空格,也可没有空格
*****************************************************************************/
#include <string>
#define COMMENT_CHAR '#'//注释符
using namespace std;

class operate_config
{
private :
	ifstream *infile;
public:
	operate_config(void);
	//参数filename,配置文件的名字
	operate_config(const string & filename);
	operate_config(void);
	//参数name,配置项的名字
	//返回值,对应配置项name的value值
	string getValue(const string & name);
};

operate_config.cpp:

#include "StdAfx.h"
#include "operate_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);
}

//参数name,配置项的名字
//返回值,对应配置项name的value值
string operate_config::getValue(const string & name)
{
	string line;
	string new_line;
	while (getline(*infile, line)) 
	{
		if (line.empty())
		{
			return "";
		}
		int start_pos = 0, end_pos = line.size() - 1, pos;
		if ((pos = line.find(COMMENT_CHAR)) != -1) 
		{
			if (0 == pos) 
			{  // 行的第一个字符就是注释字符
				return "";
			}
            end_pos = pos - 1;
        }
        new_line = line.substr(start_pos, start_pos + 1 - end_pos);  // 预处理,删除注释部分 
        if ((pos = new_line.find('=')) == -1)
        {
			return "";  // 没有=号
		}
		string na=new_line.substr(0, pos);
		Trim(na);
		if(na==name)
		{
			string value=new_line.substr(pos + 1, end_pos + 1- (pos + 1));
			Trim(value);
			return  value;
		}
    }
	return "";
}

operate_config::operate_config(const string & filename)
{
	infile=new ifstream(filename.c_str());
    if (!infile) 
	{
        cout << "无法打开配置文件" << endl;
    }
}

operate_config::operate_config(void)
{
}
operate_config::~operate_config(void)
{}

使用:

int _tmain(int argc, _TCHAR *argv[])
{
	int temp;
	operate_config opc("config.ini");
	cout<<opc.getValue("key2");
	scanf("%d",&temp);
    return 0;
}


使用说明:

首先将operate_config.h和operate_config.cpp加入项目
#include "operate_config.h"
然后operate_config opc("config.ini");//传入的是配置文件名字,我测试的时候在debug下放了个config.ini配置文件
    string value=opc.getValue("key2");//获取key2的值,结果为value2


测试的配置文件是config.ini

效果图:

                           

                              

  • 0
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值