C++ stl 读写ini配置文件

来源:互联网 发布:雪mm黑历史知乎 编辑:程序博客网 时间:2024/04/27 15:34

头文件(IniFile.h)
复制代码

  1. #ifndef __CINI_FILE__
    #define __CINI_FILE__
    #include <string>
    #include <vector>
    using namespace std;
    /*
    程序说明
    1.从INI文件中读取参数
    2.将参数写入INI格式文件
    3.[sectin]必须第一个字符为'[',并以']'结束
    4.';'后的字符串全部为注释
    5.参数以name=value的方式表示,每一行只能设置一个参数,'='不能含有空格
    6.没有对程序中注释做额外的处理,在写入文件时,没有考虑将注释写入INI文件
    7.关于INI文件格式定义请参考:
    [url]http://www.microsoft.com/technet/archive/wfw/0_welcom.mspx?mfr=true[/url]
    */
    //参数行

    struct CIniEntry
    {
        CIniEntry(){}
        CIniEntry(char* szName,char* szValue):m_strName(szName),m_strValue(szValue){}
        string m_strName;
        string m_strValue;
    };
    //注释行

    struct CIniComment
    {
        CIniComment(){}
        CIniComment(char* strCmt):m_strComment(strCmt){}
        string m_strComment;

    };
    //Section

    struct CIniSection
    {
        string m_isName;
        vector<CIniEntry> m_ieEntry;
        vector<CIniComment> m_icComment;
    };

    class CIniFile
    {
    public:
        //从INI文件中读取参数

        int ReadIniFile(char* szIniFile);
        //将参数写入INI文件

        int WriteIniFile(char* szIniFile);
        //删除字符串两端的空格

        int Trim(char* &szString);
        //去除字符串中的注释

        int RemoveComment(char* szLine);
    private:
        vector<CIniSection> m_isSection;
    };
    #endif

实现文件(IniFile.cpp)
复制代码
  1. #include "IniFile.h"
    #include <fstream>
    using namespace std;

    //一行最大字符数为260

    #define MAX_COLS 260

    #define E_OK                0x00L
    #define E_OPEN_FILE_FAILED    0x01L

    /*
    功能
        读取INI文件
    参数
        szIniFile    in    读入的INI文件名称
    返回值
        E_OK                调用成功
        E_OPEN_FILE_FAILED    打开文件错我
    */
    int CIniFile::ReadIniFile(char* szIniFile)
    {
        ifstream fIniFile(szIniFile);
        if(fIniFile == NULL)
        {
            return E_OPEN_FILE_FAILED;
        }
        char szLine[MAX_COLS] = {0};
        while (fIniFile.getline(szLine,MAX_COLS))
        {
            char* p = szLine;
            //是否为[]

            if( *p == '[')
            {
                RemoveComment( p );
                char* pEnd = strchr( p ,']');
                if( pEnd == NULL)
                    continue;
                *pEnd = 0;
                CIniSection is;
                is.m_isName = string( p + 1 );
                m_isSection.push_back(is);
                continue;
            }
            //是否为;

            Trim( p );
            if( *p == ';')
            {
                m_isSection[m_isSection.size() - 1].m_icComment.push_back(p + 1);
                continue;
            }
            //否则视为entry

            //p = szLine;

            char* pTemp = strchr( p,'=');
            if(pTemp == NULL)
            {
                continue;
            }
            *pTemp = 0;
            //创建一个Entry

            CIniEntry ie;
            ie.m_strName = p ;
            ie.m_strValue = pTemp + 1;
            //将Entry加入到响应的Section

            m_isSection[m_isSection.size() - 1 ].m_ieEntry.push_back(ie);

            memset(szLine,0,MAX_COLS);
        }
        fIniFile.close();

        return E_OK;
    }
    /*
    功能
        将CIniFile中的内容写入文件
    参数
        szIniFile    in    生成的INI文件名称
    返回值
        E_OK        调用成功
        E_OPEN_FILE_FAILED    打开文件错误
    */
    int CIniFile::WriteIniFile(char* szIniFile)
    {
        ofstream fIniFile(szIniFile);
        if(fIniFile == NULL)
            return E_OPEN_FILE_FAILED;
        for (size_t i = 0; i < m_isSection.size();++i)
        {
            fIniFile.write("[",1);
            fIniFile.write(m_isSection[i].m_isName.c_str(),m_isSection[i].m_isName.length());
            fIniFile.write("]",1);
            fIniFile << endl;
            for (size_t j = 0; j < m_isSection[i].m_ieEntry.size();++ j)
            {
                fIniFile.write(m_isSection[i].m_ieEntry[j].m_strName.c_str(),m_isSection[i].m_ieEntry[j].m_strName.length());
                fIniFile.write("=",1);
                fIniFile.write(m_isSection[i].m_ieEntry[j].m_strValue.c_str(),m_isSection[i].m_ieEntry[j].m_strValue.length());
                fIniFile << endl;
            }
        }
        fIniFile.close();
        return E_OK;
    }
    /*
    功能
        删除前后的空格(' ','/t','/r','/n')
    参数
        szString        in    传入的字符串
                        out 去除空格后的字符串
    返回值
        E_OK    调用成功
    */
    int CIniFile::Trim(char* &szString)
    {
        char* p = szString;
        while (*p == ' ' || *p == '/t')
        {
            p ++;
        }
        szString = p;

        p = szString + strlen(szString) - 1;
        while ( *p == ' ' || *p == '/t' || *p == '/r' || *p == '/n')
        {
            -- p;
        }
        *( p + 1 ) = 0;
       

        return E_OK;
    }
    /*
    功能
        删除注释
    参数
        szLine    in    传入的字符串
                out 删除注释后的字符串
    返回值
        E_OK    调用成功
    */
    int CIniFile::RemoveComment(char* szLine)
    {
        char* p = strchr(szLine,';');
        if( p == NULL)
            return 0;
        *p = 0;
        return 0;
    }

测试代码

复制代码
  1.   CIniFile cf;
        cf.ReadIniFile("IniFile.ini");
        cf.WriteIniFile("IniFile2.ini");

原创粉丝点击