【QT】文件读写操作

读取输出:

    QFile file("D:/Englishpath/QTprojects/1.dat");
    if(!file.open(QIODevice::ReadOnly)) 
    {
        qDebug()<<"Can't open the file!"<<endl;
    }
    QTextStream in(&file);
    while( !in.atEnd())
    {
        QString line = in.readLine();
        qDebug() << line;
    }

 或

   QFile file("D:/Englishpath/QTprojects/1.dat");
    if(!file.open(QIODevice::ReadOnly))
    {
        qDebug()<<"Can't open the file!"<<endl;
    }
    QTextStream stream(&file);
    QString line_in;
    stream.seek(file.size());//将当前读取文件指针移动到文件末尾
    int count = 0;
    while(count < 10)
    {
        stream << QObject::trUtf8("新建行:") <<++count<<"/n";
    }
    stream.seek(0);//将当前读取文件指针移动到文件开始
    while( !stream.atEnd())
    {
        line_in = stream.readLine();
        qDebug() << line_in;
    }

【转载自】

Qt之文件操作 - liuhongwei123888的专栏 - CSDN博客 https://blog.csdn.net/liuhongwei123888/article/details/6084761

猜你喜欢

转载自www.cnblogs.com/wxl845235800/p/10770599.html