my-QT专栏

QT文件操作记住上次读取的位置

2021-06-27  本文已影响0人  c之气三段
//记住上一次位置,可扩展于指定行
   QFile file("D:/Users/13054/Desktop/mm.txt");
    if(file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
         qint64 loca;
         QTextStream in(&file);
         loca = in.pos();
         QString s;
         while(!in.atEnd())
         {
             in.seek(loca);
             s=in.readLine();
             qDebug()<<s;
             loca=in.pos();
         }        
         file.close();
    }
//逐个有效单词读取
    QFile file("D:/Users/13054/Desktop/grid.txt");
        if(file.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            QTextStream in(&file);
             while(!in.atEnd())
             {
                 QString s;
                 in>>s;
                 qDebug()<<s;
             }
             file.close();
        }
//根据每行有效字符串数量处理
QFile file("D:/Users/13054/Desktop/grid.txt");
    if(file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        QTextStream in(&file);
        while(!in.atEnd())
        {
            QString strLine=in.readLine();
            QTextStream line(&strLine);
            QStringList list;
            while(!line.atEnd())
            {
                QString s;
                line>>s;
                list<<s;
                qDebug()<<list.size();
            }
        }
        file.close();
    }
//排除注释影响
void StartWork::startWorkSystem(const string &filePath)
{
    ifstream out(filePath, ios::in|ios::binary);
    if (!out.is_open()) {
        cout<<"No file found"<<endl;
        return;
    }
    string line;
    while(!out.eof())
    {
        getline(out,line);
        line = line.substr(0,line.find("//"));
        cout<<line<<endl;
   }
    out.close();
}
    // 输出信息至文件中(读写、追加形式)
    QFile file("log.txt");
    file.open(QIODevice::ReadWrite | QIODevice::Append);
    QTextStream stream(&file);
    stream << strMessage << "\r\n";
    file.flush();
    file.close();
上一篇下一篇

猜你喜欢

热点阅读