QT basic tutorial (file operations in QT)


Preface

In this article, we will explain the file operations in QT. File operations are a very important point for QT, so the following words will explain the file operations in QT in detail.

1. File operation method

In QT, file operations are accomplished through Qt's file and directory handling classes. The following are some commonly used file operation functions:

1. Open and close files:
Files can be opened and closed in QT using the QFile class. The sample code is as follows:

#include <QFile>

// 打开文件
QFile file("path/to/file.txt");
if (file.open(QIODevice::ReadOnly)) {
    
    
    // 文件已成功打开,可以进行读取操作
    // ...
    // 关闭文件
    file.close();
}

Text files can be read and written using the QTextStream class. The sample code is as follows:

#include <QFile>
#include <QTextStream>

// 打开文件
QFile file("path/to/file.txt");
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
    
    
    QTextStream in(&file);
    
    // 逐行读取文件内容
    while (!in.atEnd()) {
    
    
        QString line = in.readLine();
        // 处理读取到的每一行数据
        // ...
    }
    
    // 关闭文件
    file.close();
}

2. Write to a file:
Use the QFile class and the QTextStream class to write text to a file. The sample code is as follows:

#include <QFile>
#include <QTextStream>

// 打开文件
QFile file("path/to/file.txt");
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
    
    
    QTextStream out(&file);
    
    // 写入数据到文件
    out << "Hello, World!" << endl;
    
    // 关闭文件
    file.close();
}

3. File copying and moving:
Use the copy() function of the QFile class to copy files, and the rename() function to move or rename files. The sample code is as follows:

#include <QFile>

// 复制文件
QString sourceFilePath = "path/to/source.txt";
QString destinationFilePath = "path/to/destination.txt";
QFile::copy(sourceFilePath, destinationFilePath);

// 移动或重命名文件
QString filePath = "path/to/file.txt";
QString newFilePath = "path/to/newfile.txt";
QFile::rename(filePath, newFilePath);

4. Delete files:
Use the remove() function of the QFile class to delete files. The sample code is as follows:

#include <QFile>

// 删除文件
QString filePath = "path/to/file.txt";
QFile::remove(filePath);

These are commonly used file operation functions in QT. You can use appropriate classes and functions according to specific needs to perform operations such as opening, reading, writing, copying, moving, and deleting files. When performing file operations, remember to close the file at the appropriate time to release system resources.

2. QFileInfo class

The QFileInfo class is a class in Qt used to obtain and operate file information. It provides many convenient methods that allow you to easily obtain various properties and metadata about files.

You can use the QFileInfo class to obtain the file's name, path, size, modification date, access permissions and other information. It can also determine whether the file exists, whether it is a directory, and determine the type and suffix of the file.

The following are some commonly used functions and usage of the QFileInfo class:

1. Constructor:
The QFileInfo class has multiple constructors that can be used. The most common is to create a QFileInfo object by passing in a file path.

QFileInfo fileInfo("path/to/file.txt");

2. File information query:
Use the fileName() function to obtain the file name.

QString fileName = fileInfo.fileName();

Use the filePath() function to get the full path of a file.

QString filePath = fileInfo.filePath();

Use the size() function to get the size of a file in bytes.

qint64 fileSize = fileInfo.size();

Use the lastModified() function to get the last modified date and time of a file.

QDateTime lastModified = fileInfo.lastModified();

3. File status query:

Use the exists() function to check whether the file exists.

bool fileExists = fileInfo.exists();

Use the isFile() function to check whether the file is an ordinary file.

bool isRegularFile = fileInfo.isFile();

Use the isDir() function to check whether the file is a directory.

bool isDirectory = fileInfo.isDir();

4. File access permission query:
Use the isReadable() function to check whether the file is readable.

bool isReadable = fileInfo.isReadable();

Use the isWritable() function to check whether the file is writable.

bool isWritable = fileInfo.isWritable();

5. File type query:
Use the suffix() function to get the suffix name of the file.

QString suffix = fileInfo.suffix();

Use the completeSuffix() function to get the complete suffix of the file, including all extensions.

QString completeSuffix = fileInfo.completeSuffix();

4. QTemporaryFile class

QTemporaryFile is a class in Qt for creating temporary files. Temporary files are files used to temporarily store data while an application is running, and are usually automatically deleted when no longer needed.

The QTemporaryFile class inherits from the QFile class, so it has all the functions of the QFile class, and also provides some additional methods to handle the creation, automatic deletion and other features of temporary files.

The following are some common methods and usage of the QTemporaryFile class:

1. Constructor:
The QTemporaryFile class has multiple constructors, which can use different parameter forms to create temporary files. The most commonly used is the default constructor.

QTemporaryFile tempFile;

2. File operations:
Use the open() function to open temporary files for reading and writing operations.

if (tempFile.open()) {
    
    
    // 文件操作代码
}

Use the write() function to write data to a temporary file.

QByteArray data = "Hello, world!";
tempFile.write(data);

Use the readAll() function to read the contents of the entire temporary file at once.

QByteArray fileData = tempFile.readAll();

3. File status and attributes:
Use the fileName() function to obtain the path of the temporary file.

QString filePath = tempFile.fileName();

Use the size() function to get the size of a temporary file in bytes.

qint64 fileSize = tempFile.size();

4. Automatic deletion:
Use the autoRemove() function to set whether temporary files are automatically deleted when destroyed.

tempFile.setAutoRemove(true); // 默认值为true,表示自动删除文件

When the temporary file object (QTemporaryFile) goes out of scope or is destructed, if automatic deletion is set, the file will be automatically deleted. You can also manually call the remove() function to manually delete temporary files.

tempFile.remove(); // 手动删除文件

The above are just some common methods and usage of the QTemporaryFile class. You can check the official Qt documentation for more details and examples about the QTemporaryFile class and other available functions.

Summarize

This article will explain it here.

Guess you like

Origin blog.csdn.net/m0_49476241/article/details/132951141