189 8069 5689

c++对properties配置文件操作工具类

源代码GitHub路径:源代码地址下载

最近要使用c++对windows api相关接口的封装,有2个接口要求读写properties文件。
原以为网上应该有一大堆资料的,结果拜BAI度的大恩大德,一点相关的资料都没有。那就只能自己动手丰衣足食。再次感谢十分强大只是找不到相关资料的BAI度。

创新互联建站专注于伊川网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供伊川营销型网站建设,伊川网站制作、伊川网页设计、伊川网站官网定制、小程序定制开发服务,打造伊川网络公司原创品牌,更为您提供伊川网站排名全网营销落地服务。

头文件

#pragma once

#ifndef CProperties_H
#define CProperties_H


#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

#define  IS_OK 1
#define  IS_ERROR 0
typedef  bool PSTATUS;
class CProperties{
private:
    string path;
protected:
public:
    CProperties();
    virtual ~CProperties();
    //打开文件
    PSTATUS open(const char* path);
    //实现文件加载到map中
    PSTATUS load();
    void print();
    void close();
    vector read(const char *k);
    PSTATUS write(const char *k,const char* v);
    void trim_first(string &s);
    void trim_end(string &s);
    void trim(string &s);
};

#endif

# 源文件

#include "stdafx.h"
#include "properties.h"
/************************************************************************************************/
///*
//*作者:王邵华
//*email:loveofprogram@sina.com
//*时间:20180404
//*版本历程:
//*版本号    参与人      事项    
//*V1.0.0.1  王邵华      创建  
//***************************************************************************************************
//***************************************************************************************************
//*本工具类实现c++对properties文件的读写操作,
//*1.支持单行注释如:行首除去空格后以 //、#、 开始的
//* //这是单行注释
//* #这是单行注释
//* 
//*2.支持节点 但节点并没有意义 [section]
//*[section0]
//*key=value
//*[section1]
//*key=value
//*用到multimap所以存在key键有2个值而已,节点并没有起到区分的作用,这一点也符合properties配置文件要求规范的
//*3.支持多行注释 /*第一行,第二行.....*/
//*案例(1)同行有/*..*/ 如:key=123/*oooooo*/b=890  结果保留key=123
//*案例(2)多行有/*..*/ 如:key=123/*ooo
//*0000000000000000000
//*ooo*/b=890  结果保留key=123
//*案例(3)行头行尾有/*..*/如:/*ooooo*/key=123 结果不保留
//*
//*/
/************************************************************************************************/
static vector vLine;
static multimap msKV;
static bool mulremark = false;//多行注释开关

CProperties::CProperties(){};
CProperties::~CProperties(){};

PSTATUS CProperties::open(const char* path){
    /**************************************************************/
    //判断是否是文本文档 而不是文件夹 此代码存在问题一直hFind = 0xFFFFFFFF
    //暂留 以便日后优化
    //author:王邵华
    //time:  20180404
    /*WIN32_FIND_DATA fd; 
    bool ret = true;
    HANDLE hFind = FindFirstFile(LPCWSTR(path), &fd); 
    if ((hFind != INVALID_HANDLE_VALUE) && !(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) 
    { 
        ret = false;
    }
    FindClose(hFind); 
    if (ret)
    {
        //文件属性不对
        return IS_ERROR;
    }*/
    /**************************************************************/
    if (nullptr == path)
    {
        return IS_ERROR;
    }
    this->path = path;
    ifstream ifs;
    ifs.open(path,ios::in);
    if (!ifs)
    {
        return IS_ERROR;//打开文件失败
    }
    string sLine;
    while(!ifs.eof()){
        sLine = "";
        getline(ifs,sLine);
        if (mulremark)//已打开多行注释开关 需判断该行有没有关闭开关
        {
            if(sLine.find("*/") != string::npos){
                mulremark = false;
            }
            continue;//无论开关是否关闭 继续读下一行数据
        }else
        {
            string::size_type pos =sLine.find("/*");string sSubLine;
            if (pos != string::npos)//改行有多行注释开关  需打开
            {
                string::size_type epos = sLine.rfind("*/");
                mulremark = epos==string::npos || epos2 && sSubLine[0] == '/' && sSubLine[1] == '/')continue;
            if (sSubLine.length()>4 && sSubLine[0] == '<' && sSubLine[1] == '!')continue;
            vLine.push_back(sSubLine);
        }
    }
    if (ifs.is_open())
    {
        ifs.close();
    }
    return IS_OK;
}
//实现文件加载到map中
PSTATUS CProperties::load(){
    string key,value;string sSubStr;
    for (int i = 0;i::iterator  itr = msKV.begin();
    cout<<"################################################################################"<first.c_str()<<";value:"<second.c_str()< CProperties::read(const char *k){
    vector vVauleCol;
    if (msKV.size()>0)
    {
        multimap::size_type  cnt = msKV.count(k);
        multimap::iterator  iter = msKV.find(k);
        for(;cnt > 0; cnt--, iter++)
        {
            vVauleCol.push_back(iter->second);
        }
    }
    return vVauleCol;
}

/*
*aim:将key=value追加到文本末尾,更新multimap映射插入
*/
PSTATUS CProperties::write(const char *k,const char* v){
    if (nullptr == k || nullptr == v)
    {
        return IS_ERROR; //校验入参
    }
    ofstream ofs;
    ofs.open(this->path.c_str(),ios::app);
    if(!ofs)
    {
        return IS_ERROR;//打开文件失败
    }
    char sStr[1024] = {};
    sprintf(sStr,"%s=%s",k,v);
    ofs<

调用实例

int _tmain1(int argc, _TCHAR* argv[])
{

//########################################################//
//read
//1.创建对象open()文件
//2.load()加载到内存中
//3.read()读取相关key值的value
//4.close()释放资源

CProperties cprop;
PSTATUS ret = cprop.open("D:\\job\\greatwall\\test\\bank.properties");
if (ret != IS_OK)
{
    cout<<"打开配置文件失败"< vec = cprop.read("key3");
for (int i=0; i

//########################################################//

//########################################################//
//write
//1.创建对象
//2.load()加载到内存中
//3.write()读取相关key值的value
//4.close()释放资源

CProperties cprop_write;
ret = cprop_write.open("D:\\job\\greatwall\\test\\bank2.properties");
cprop_write.write("aaa","bbb");
cprop_write.write("aaa","bbb");
vec = cprop_write.read("aaa");
for (int i=0; i

新闻名称:c++对properties配置文件操作工具类
当前URL:http://cdxtjz.cn/article/pgcgco.html

联系我们

您好HELLO!
感谢您来到成都网站建设公司,若您有合作意向,请您为我们留言或使用以下方式联系我们, 我们将尽快给你回复,并为您提供真诚的设计服务,谢谢。
  • 电话:028- 86922220 18980695689
  • 商务合作邮箱:631063699@qq.com
  • 合作QQ: 532337155
  • 成都网站设计地址:成都市青羊区锣锅巷31号五金站写字楼6楼

小谭建站工作室

成都小谭网站建设公司拥有多年以上互联网从业经验的团队,始终保持务实的风格,以"帮助客户成功"为已任,专注于提供对客户有价值的服务。 我们已为众企业及上市公司提供专业的网站建设服务。我们不只是一家网站建设的网络公司;我们对营销、技术、管理都有自己独特见解,小谭建站采取“创意+综合+营销”一体化的方式为您提供更专业的服务!

小谭观点

相对传统的成都网站建设公司而言,小谭是互联网中的网站品牌策划,我们精于企业品牌与互联网相结合的整体战略服务。
我们始终认为,网站必须注入企业基因,真正使网站成为企业vi的一部分,让整个网站品牌策划体系变的深入而持久。