加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

Qt5.7读写中文ini配置文件

(2016-12-11 22:24:50)
标签:

杂谈

      近期为方便工作,开始把Qt作为主要的开发工具了,但是Qt自带的读写ini的方法不支持中文,而且ini文件的格式也不是ansi,非常不方便与自己之前写的程序进行交互,网上也没有相关的Qt读写中文ini的文档,因此决定自己造轮子,断断续续的弄了两天算是搞定了,下面把代码贴出来,供各位有需求的朋友参考。

    说明:本文档共含有4个函数,从前往后依次是读取函数(ReadINI),写入函数(WriteINI),删除节点函数(DeleteINI_Section),删除键值函数(DeleteINI_Key)。其中读取函数的算法是其他几个函数的基础,大致算法如下:

     以读取文本的方式载入ini文件到一个字符串中,获取[关键字]的位置,再获取[关键字]之后第一个[的位置,截取这两个位置之间的字符串,就获得了一个节(Section)的内容,然后再通过搜索截取后的字符串的关键字“\r\n关键字=”右侧的字符串读取该键值。写入、修改、删除操作同理,只要理解了读取原理,这些也不难。需要特殊说明的是, "[--最后一行用来做标记的一串,写入文件时再删掉--]"这种类似的字符串,顾名思义,是为了方便标记,加到字符串末尾的,以方便最后一个节的内容的截取。

QString MainWindow::ReadINI(QString ObjSection,QString ObjKey,QString ObjPath)

{

    QFile myfile(ObjPath);

    myfile.open(QIODevice::ReadOnly);

      QTextStream infile(&myfile);

    QString ini_all = infile.readAll(); //以文本方式读入路径下的ini文件,存入ini_all字符串

    ini_all.replace(QRegExp(" "), "");//替换掉ini_all内的所有空格

    QString SectionValue; //存放某一节点所有内容的字符串

    QString KeyValue; //存放某一键的键值的字符串

    QString FindValue;//想要查找的键值

    FindValue =  "[" ObjSection "]"; //以[]加节点名称用于搜索,防止如哈、哈哈等相似的匹配错误

    int SectionFindStart = ini_all.indexOf(FindValue);//[节点]为起始点

    ini_all.append("\r\n[这是一串用来解决问题的没有名字的字符串]\r\n");//添加到字符串末尾

    ini_all.toLocal8Bit();//我也不知道这个转换用没有起到作用

    int SectionFindEnd = ini_all.indexOf("[",SectionFindStart 1);//下一个节点的"["为结束点

    SectionValue = ini_all.mid(SectionFindStart,SectionFindEnd-SectionFindStart);//获取节点内容

    int KeyFindStart =SectionValue.indexOf("\r\n" ObjKey "=");//以"\r\n键值="作为条件匹配

    if( KeyFindStart != -1) //如果搜索到匹配的键值,则读取键值内容

    {

        KeyFindStart =SectionValue.indexOf("=",KeyFindStart);//以等号作为起始点

        int KeyFindEnd = SectionValue.indexOf("\r\n",KeyFindStart);//以等号后的换行符作为结束点

        KeyValue = SectionValue.mid(KeyFindStart 1,KeyFindEnd - KeyFindStart-1);//截取键值

        KeyValue.replace(QRegExp("\r"), "");//替换掉换行符

        KeyValue.replace(QRegExp("\n"), "");//替换掉换行符

        return KeyValue;//返回键值

      }

   myfile.close();

   infile.reset();

}


void MainWindow::WriteINI(QString ObjSection, QString ObjKey, QString ObjText, QString ObjPath)

{

    QString ExportINI;//用来存放导出的ini字符串

    QString ExportCopy;//用来存放导出的ini字符串备份

    QString SectionValue; //存放某一节点所有内容的字符串

    QString SectionValue1; //存放某一节点所有内容的字符串备份

    QString KeyValue; //存放某一键的键值的字符串

    QString FindValue;//想要查找的键值

    QFileInfo CheckFile(ObjPath);

    if (CheckFile.isFile()) //若目标路径存在,则导入内容

    {

        QFile ReadMyfile(ObjPath);

        ReadMyfile.open(QIODevice::ReadOnly);

        QTextStream infile(&ReadMyfile);

        ExportINI = infile.readAll(); //以文本方式读入路径下的ini文件,存入ExportINI字符串

         ExportINI.replace(QRegExp(" "), "");

         ExportCopy = ExportINI;

        ReadMyfile.close();

    }

    FindValue =  "[" ObjSection "]"; //以[]加节点名称用于搜索,防止如哈、哈哈等相似的匹配错误

    int SectionFindStart = ExportINI.indexOf(FindValue);//[节点]为起始点

    ExportINI.toLocal8Bit();//我也不知道这个转换用没有起到作用

    ExportINI.append("[--最后一行用来做标记的一串,写入文件时再删掉--]");

    int SectionFindEnd = ExportINI.indexOf("[",SectionFindStart 1);//下一个节点的"["为结束点

    SectionValue = ExportCopy.mid(SectionFindStart,SectionFindEnd-SectionFindStart);//获取节点内容

    SectionValue1 =  SectionValue;

    int KeyFindStart =SectionValue.indexOf("\r\n" ObjKey "=");//以"\r\n键值="作为条件匹配

    if (SectionFindStart != -1)

    {

        if( KeyFindStart != -1) //如果搜索到匹配的键值,则替换键值内容

        {

            KeyFindStart =SectionValue.indexOf("=",KeyFindStart);//以等号作为起始点

            int KeyFindEnd = SectionValue.indexOf("\r\n",KeyFindStart);//以等号后的换行符作为结束点

            QString KeyValue = SectionValue.mid(KeyFindStart 1,KeyFindEnd - KeyFindStart-1);//截取键值

            KeyValue.replace(QRegExp("\r"), "");//替换掉换行符

            KeyValue.replace(QRegExp("\n"), "");//替换掉换行符

            SectionValue.replace(ObjKey "=" KeyValue,ObjKey "=" ObjText);//替换节内键值

            ExportINI.replace(SectionValue1,SectionValue);//替换ini内节的值

        }

        else //若无匹配键值,则写入键及键值

        {

            ExportINI = ExportINI.insert(SectionFindEnd, ObjKey "=" ObjText "\r\n");

        }



    }

    else //没有匹配的节,则写入节及相关键值

    {

        ExportINI = ExportINI FindValue "\r\n" ObjKey "=" ObjText "\r\n";

    }


     ExportINI.replace("[--最后一行用来做标记的一串,写入文件时再删掉--]", "");

    QFile myfile(ObjPath);

    myfile.open(QIODevice::WriteOnly);

    QTextStream infile(&myfile);

    infile << ExportINI; //相关的覆盖ini文件

    myfile.close();

}

void MainWindow::DeleteINI_Section(QString ObjSection,QString ObjPath)

{

    QString ExportINI;//用来存放导出的ini字符串

    QString ExportCopy;//用来存放导出的ini字符串备份

    QString SectionValue; //存放某一节点所有内容的字符串

    QString SectionValue1; //存放某一节点所有内容的字符串备份

    QString KeyValue; //存放某一键的键值的字符串

    QString FindValue;//想要查找的键值

    QFileInfo CheckFile(ObjPath);

    if (CheckFile.isFile()) //若目标路径存在,则导入内容

    {

        QFile ReadMyfile(ObjPath);

        ReadMyfile.open(QIODevice::ReadOnly);

        QTextStream infile(&ReadMyfile);

        ExportINI = infile.readAll(); //以文本方式读入路径下的ini文件,存入ExportINI字符串

         ExportINI.replace(QRegExp(" "), "");

         ExportCopy = ExportINI;

        ReadMyfile.close();

    }

    FindValue =  "[" ObjSection "]"; //以[]加节点名称用于搜索,防止如哈、哈哈等相似的匹配错误

    int SectionFindStart = ExportINI.indexOf(FindValue);//[节点]为起始点

    ExportINI.toLocal8Bit();//我也不知道这个转换用没有起到作用

    ExportINI.append("[--最后一行用来做标记的一串,写入文件时再删掉--]");

    int SectionFindEnd = ExportINI.indexOf("[",SectionFindStart 1);//下一个节点的"["为结束点

    SectionValue = ExportCopy.mid(SectionFindStart,SectionFindEnd-SectionFindStart);//获取节点内容

    SectionValue1 =  SectionValue;


    if (SectionFindStart != -1)

    {

            ExportINI.replace(SectionValue,"");//替换ini内节的值

    }


     ExportINI.replace("[--最后一行用来做标记的一串,写入文件时再删掉--]", "");

    QFile myfile(ObjPath);

    myfile.open(QIODevice::WriteOnly);

    QTextStream infile(&myfile);

    infile << ExportINI; //相关的覆盖ini文件

    myfile.close();

}

void MainWindow::DeleteINI_key(QString ObjSection, QString ObjKey,QString ObjPath)

{

    QString ExportINI;//用来存放导出的ini字符串

    QString ExportCopy;//用来存放导出的ini字符串备份

    QString SectionValue; //存放某一节点所有内容的字符串

    QString SectionValue1; //存放某一节点所有内容的字符串备份

    QString KeyValue; //存放某一键的键值的字符串

    QString FindValue;//想要查找的键值

    QFileInfo CheckFile(ObjPath);

    if (CheckFile.isFile()) //若目标路径存在,则导入内容

    {

        QFile ReadMyfile(ObjPath);

        ReadMyfile.open(QIODevice::ReadOnly);

        QTextStream infile(&ReadMyfile);

        ExportINI = infile.readAll(); //以文本方式读入路径下的ini文件,存入ExportINI字符串

         ExportINI.replace(QRegExp(" "), "");

         ExportCopy = ExportINI;

        ReadMyfile.close();

    }

    FindValue =  "[" ObjSection "]"; //以[]加节点名称用于搜索,防止如哈、哈哈等相似的匹配错误

    int SectionFindStart = ExportINI.indexOf(FindValue);//[节点]为起始点

    ExportINI.toLocal8Bit();//我也不知道这个转换用没有起到作用

    ExportINI.append("[--最后一行用来做标记的一串,写入文件时再删掉--]");

    int SectionFindEnd = ExportINI.indexOf("[",SectionFindStart 1);//下一个节点的"["为结束点

    SectionValue = ExportCopy.mid(SectionFindStart,SectionFindEnd-SectionFindStart);//获取节点内容

    SectionValue1 =  SectionValue;

    int KeyFindStart =SectionValue.indexOf("\r\n" ObjKey "=");//以"\r\n键值="作为条件匹配

    if (SectionFindStart != -1)

    {

        if( KeyFindStart != -1) //如果搜索到匹配的键值,则替换键值内容

        {

            KeyFindStart =SectionValue.indexOf("=",KeyFindStart);//以等号作为起始点

            int KeyFindEnd = SectionValue.indexOf("\r\n",KeyFindStart);//以等号后的换行符作为结束点

            QString KeyValue = SectionValue.mid(KeyFindStart 1,KeyFindEnd - KeyFindStart-1);//截取键值

            KeyValue.replace(QRegExp("\r"), "");//替换掉换行符

            KeyValue.replace(QRegExp("\n"), "");//替换掉换行符

            SectionValue.replace(ObjKey "=" KeyValue,"");//替换节内键值

            ExportINI.replace(SectionValue1,SectionValue);//替换ini内节的值

        }

    }


    ExportINI.replace("[--最后一行用来做标记的一串,写入文件时再删掉--]", "");

    QFile myfile(ObjPath);

    myfile.open(QIODevice::WriteOnly);

    QTextStream infile(&myfile);

    infile << ExportINI; //相关的覆盖ini文件

    myfile.close();

}

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有