How to use the QFile class to open files in Qt

Introduction to Qt file operation class QFile

Qt uses the QFile class to manipulate file input/output. Inherited from QIODevice, the QIODevice class is the base class of input/output devices,

Provides public implementations and abstract interfaces for devices to read and write block data. QIODevice inherits from QObject.

1. Use the QFile class to open the file

Constructor of QFile

QFile(const QString &name) //pass in a file path

After the construction is completed, the file is not opened, and the QFile::open function needs to be used to open the file

[virtual] bool QFile::open(OpenMode mode);
/*
*OpenMode mode 打开方式,是一个枚举类型
*QIODevice::NotOpen 不打开
*QIODevice::ReadOnly 只读方式
*QIODevice::WriteOnly 读写方式
*QIODevice::ReadWrite 读写方式
*QIODevice::Append   追加方式
*QIODevice::Truncate 阶段方式
*QIODevice::Text     转换不同平台的换行,读的时候把所有换行转成'\n',写的时候再把'\n'转换对应平台的换行
*QIODevice::Unbuffered 不使用缓冲区
*/

For example:

QFile file("d:/123.txt");
file.open(QIODevice::ReadOnly);

2. The QFile class closes the file

[virtual] void QFileDevice::close(); //刷新缓冲区,并关闭文件

3. QFile class file read operation

QIODevice::read function

QByteArray QIODevice::read(qint64 maxSize);//读取maxSize个字节,内部位置指针后移maxSize,并返回一个QByteArray对象。

For example:

QFile file("d:/123.txt");
file.open(QIODevice::ReadOnly);
qDebug() << file.read(10) << endl;
file.close();

QIODevice::readLine function

QByteArray QIODevice::readLine(qint64 maxSize = 0) //读取一行,但是这一行不能超过maxSize字节,maxSize = 0代表不限制行字节数。

For example:

QFile file("d:/123.txt");
file.open(QIODevice::ReadOnly);
qDebug() << file.readLine(10) << endl; 
file.close();

QIODevice::readAll function

QByteArray QIODevice::readAll()

4. QFile class file write operation

QIODevice::write function

qint64 QIODevice::write(const QByteArray &byteArray); //将byteArray写入文件,写完内部位置指针后移

For example:

QFile file("d:/123.txt");
file.open(QIODevice::ReadWrite | QIODevice::Text); //打开模式可以使用‘|'组合
QByteArray byte("hellworld");
file.write(byte);
file.write(byte);
file.close();

5、QDataStream

Flow control file input and output can use QDataStream.

flow control write

#include <QDataStream>
#include <QDebug>
#include <QFile>
int main(int argc, char**argv)
{
    QFile file("d:/123.txt");
    file.open(QIODevice::ReadWrite);
    QDataStream stream(&file);
    int a = 10;
    QString str = "helloworld";
    stream << a << str;
    file.close();
    return 0;
}

Fluidic read

#include <QDataStream>
#include <QDebug>
#include <QFile>
int main(int argc, char**argv)
{
    QFile file("d:/123.txt");
    file.open(QIODevice::ReadWrite);

    QDataStream stream(&file);
    int a;
    QString str;
    stream >> a >> str;
    qDebug() << "a:" << a << "str:" << str << endl;
    file.close();
    return 0;
}

The benefits of this article, free to receive Qt development learning materials package, technical video, including (C++ language foundation, C++ design pattern, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project actual combat, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓See below↓↓Click on the bottom of the article to receive the fee↓↓

Guess you like

Origin blog.csdn.net/m0_73443478/article/details/130730323