QT开发之使用ini文件读存中文

1、头文件

#include <QFontDatabase>
#include <QFileInfo>
#include <QFile>
#include <QTextStream>

2、声明

QString ReadINI(QString ObjSection, QString ObjKey, QString ObjPath);
    void WriteINI(QString ObjSection, QString ObjKey, QString ObjText, QString ObjPath);

3、代码

QString loaddialog::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 loaddialog::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();
}

4、使用

//写入,没有config.ini就自动创建
WriteINI("123", "456", "中国人", "D:/config.ini");
//显示
ui->lineEdit->setText(ReadINI("123", "456", "D:/config.ini"));

 

5、其它:删除节点函数(DeleteINI_Section),删除键值函数(DeleteINI_Key)

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();

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值