读取输出:

    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;
    }

 或

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
   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;
    }

 

 

写入

int num = 0x12345678;
QFile file("D:/Englishpath/QTprojects/test_qt.txt");
file.open(QIODevice::ReadWrite);
QDataStream in(&file);
qDebug() << "num_1 = " << num;
in << num;
file.seek(0);
in >> num;
qDebug() << "num_2 = " << num;
file.close();

【QT】文件读写操作 随笔 第1张

【QT】文件读写操作 随笔 第2张

【QT】文件读写操作 随笔 第3张

 

 

【转载自】

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

Qt与C文件操作的差异 - caploveleo的专栏 - CSDN博客 https://blog.csdn.net/caploveleo/article/details/9148217

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄