C++读取配置文件之代码实现

这是一个C++实现的配置文件读取类,通过构造函数打开文件,析构函数关闭,提高读取效率。类中包含ReadConfig方法用于根据键获取配置值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

读取配置文件参考  http://www.cnblogs.com/tzhangofseu/archive/2011/10/02/2197966.html

修改成类的形式,并且在构造函数里面打开,析构函数关闭,不用每次读取一行数据的时候都打开文件,提高效率!

/**
 * @file config.h
 *
 *  日期: 2013-3-20
 *  @author 张露
 */


#ifndef CONFIG_H_
#define CONFIG_H_



#define COMMENT_CHAR '#'
#define CONFIG_NAME "../config/config.ini"
/**
* @brief Config类
* 读取配置文件的操作
*/
class Config
{
public:
Config(const std::string filename);

Config(const Config& c);

~Config();

bool ReadConfig(const std::string& key, std::string& keyvalue);

private:
///文件名称
    std::string m_filename;


    std::ifstream m_infile;
};

#endif


 /*
 * config.cpp
 *
 *  日期: 2013-3-23
 *  作者: 张露
 */


#include "stdafx.h"


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;
    }
    string::size_type 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;
    string::size_type start_pos = 0, end_pos = line.size() - 1, pos = string::npos;
    if ((pos = line.find(COMMENT_CHAR)) != string::npos) {
        if (0 == pos) {  // 行的第一个字符就是注释字符
            return false;
        }
        end_pos = pos - 1;
    }
    string new_line = line.substr(start_pos, end_pos+1-start_pos);  // 预处理,删除注释部分
    
    if ((pos = new_line.find('=')) == string::npos)
        return false;  // 没有=号
        
    string sKey = new_line.substr(0, pos);
Trim(sKey);
if (0 != sKey.compare(key))
return false;

    value = new_line.substr(pos + 1, end_pos + 1- (pos + 1));
    Trim(key);
    if (key.empty()) {
        return false;
    }
    Trim(value);
    return true;
}
Config :: Config(const string filename) : m_filename(filename)
{
///每次调用构造函数的时候就打开文件
m_infile.open(m_filename.c_str());
}
Config :: Config(const Config& c) : m_filename(c.m_filename)
{

}
Config :: ~Config()
{
///析构函数时候关闭文件
if(m_infile)
{
m_infile.close();
}
}
bool Config::ReadConfig(const string& key, string& keyvalue)
{


if (!m_infile) {
return false;
}
    string line, key_,value;
    key_ = key;
    while (getline(m_infile, line)) {
        if (AnalyseLine(line, key_, value)) {
        keyvalue = value;
        return true;
        }
    }
    return false;


}




#endif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值