c++ 操作.ini 文件(适用平台linux windows)

本文介绍如何使用C++在Windows和Linux平台上读取和写入.ini配置文件,涵盖关键步骤和示例代码,实现跨平台兼容。
摘要由CSDN通过智能技术生成
       一直以来都在用windows 提供的api 来操作 .ini文件,但是当文件的条数过大时读写的速度大减,而且考虑到不能跨平台操作(linux),于是自己写了个操作 .ini文件的类,经测试,基本感觉还行,先写到我的blog中,望大家帮忙测试,多多指教,共同改善,真强其功能!
源文件:
/*
 *  project:
 *                通用模块 ( 用STL处理 *.ini 文件 )
 *                 
 *  description:
 *
 *                  通过CHandle_IniFile类操作ini 文件,实现添加、删除、修改
 *
 *          ( the end of this file have one sample,
 *                    welcom to use... )
 *
 *
 *       file:ZLB_Handle_File.h
 *
 *  author: @ zlb
 * 
 *  time:2005-07-25 
 *
 *
 * 
 --*/
 
 
 
 
#ifndef ZLB_HANDLE_FILE__
#define ZLB_HANDLE_FILE__
 
 
#include <map>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <fstream>
#include <iostream>
#include <iterator>
using namespace std;
 
typedef map<string, string, less<string> > strMap;
typedef strMap::iterator strMapIt;
 
const char*const MIDDLESTRING = "_____***_______";
 
struct analyzeini{
       string strsect;
       strMap *pmap;
       analyzeini(strMap & strmap):pmap(&strmap){}      
       void operator()( const string & strini)
       {
              int first =strini.find('[');
              int last = strini.rfind(']');
              if( first != string::npos && last != string::npos && first != last+1)
              {
                     strsect = strini.substr(first+1,last-first-1);
                     return ;
              }
              if(strsect.empty())
                     return ;
              if((first=strini.find('='))== string::npos)
                     return ;
              string strtmp1= strini.substr(0,first);
              string strtmp2=strini.substr(first+1, string::npos);
              first= strtmp1.find_first_not_of(" /t");
              last = strtmp1.find_last_not_of(" /t");
              if(first == string::npos || last == string::npos)
                     return ;
              string strkey = strtmp1.substr(first, last-first+1);
              first = strtmp2.find_first_not_of(" /t");
              if(((last = strtmp2.find("/t#", first )) != -1) ||
            ((last = strtmp2.find(" #", first )) != -1) ||
            ((last = strtmp2.find("/t//", first )) != -1)||
            ((last = strtmp2.find(" //", first )) != -1))
              {
            strtmp2 = strtmp2.substr(0, last-first);
              }
              last = strtmp2.find_last_not_of(" /t");
              if(first == string::npos || last == string::npos)
                     return ;
              string value = strtmp2.substr(first, last-first+1);
              string mapkey = strsect + MIDDLESTRING;
              mapkey += strkey;
              (*pmap)[mapkey]=value;
              return ;
       }
};
 
class CHandle_IniFile
{
public:
    CHandle_IniFile( ){};
    ~CHandle_IniFile( )
       {
              c_inimap.clear();
              vt_ini.clear();
       };
    bool open(const char* pinipath)
       {
              return do_open(pinipath);
       }
/*
 *    读取对应的值
 *    description:
 *           psect = 主键
 *           pkey  = 次级键
 *  return = value
 *
*/
    string read(const char*psect, const char*pkey)
       {
              string mapkey = psect;
              mapkey += MIDDLESTRING;
              mapkey += pkey;
              strMapIt it = c_inimap.find(mapkey);
              if(it == c_inimap.end())
                     return "";
              else
                     return it->second;
       }
/*
 *
 *  修改主键 (rootkey)
 *  description:
 *      sItself    = 要修改的主键名
 *           sNewItself = 目标主键名
 *   return = true(成功) or false(失败)
 *
*/
       bool change_rootkey(char *sItself,char *sNewItself)
       {
              if(!strcmp(sItself,"") || !strcmp(sNewItself,""))
              {
                     return false;
              }
              string sIt = "[";
              sIt+=sItself;
              sIt+="]";
              for(std::vector<string>::iterator iter = vt_ini.begin();
                     iter != vt_ini.end();iter++)
                     {
                            if(!(*iter).compare(sIt))
                            {
                                   //** 改变文件vector
                                   sIt = "[";
    
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值