QT打开txt文件,读取修改,解析数据。

#include "modify.h"
 
  
modify::modify()
{
    readFile=new QFile("E:/xwj/project/moidfy_GPgga/modifyGGA/RxRec20110607_070017.txt");
    writeFile=new QFile("E:/xwj/project/moidfy_GPgga/modifyGGA/RxRec20110607_070017.txt");
    readTxt();
}
 
  
void modify::readTxt()
{
    if(!readFile->open(QIODevice::ReadOnly|QIODevice::Text)) {
        qDebug()<<"Can't open the file!"< 
  
    }
    else
    {
        QTextStream stream(readFile);
        strAll=stream.readAll();
    }
    readFile->close();
    if(!writeFile->open(QIODevice::WriteOnly|QIODevice::Text))
    {
        qDebug()<<"Can't write the file!"< 
  
    }
    else
    {
        QTextStream stream(writeFile);
        strList=strAll.split("\n");
        for(int i=0;i<strList.count();i++)
        {
            if(i==strList.count()-1)
                         {
                             //最后一行不需要换行
                             stream<<strList.at(i);
                         }
            else if(strList.at(i).contains("$PCSI")){
                QString s;
                strListforPCSI=strList.at(i).split(",");
                    int p=strListforPCSI.at(9).toInt();
                    int len=strListforPCSI.at(9).length();
                    if(p<=13&&p>=10)
                        p+=3+qrand()%(5-3);
                    else if(p<=9&&p>=6)
                        p+=6+qrand()%(8-6);
                    else if(p<=5&&p>=2)
                        p+=10+qrand()%(13-10);
                    else if(p<=2)
                        p+=13+qrand()%(15-13);
                    s=QString::number(p);
                QString tempStr=strList.at(i);
                tempStr.replace(45,len,s);
                qDebug()<<"int p:="< 
  
                qDebug()< 
  
                qDebug()<<"int s="< 
  
                stream<'\n';
            }
            else
            {
                 stream<<strList.at(i)<<'\n';
            }
        }
    }
    readFile->close();
    qDebug()<<"OK!";
}

你可能感兴趣的:(QT打开txt文件,读取修改,解析数据。)