读取输出:
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; }
写入
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之文件操作 - liuhongwei123888的专栏 - CSDN博客 https://blog.csdn.net/liuhongwei123888/article/details/6084761
Qt与C文件操作的差异 - caploveleo的专栏 - CSDN博客 https://blog.csdn.net/caploveleo/article/details/9148217