File operations are an essential part of an application. As a common development repository, QT provides cross-platform file manipulation capabilities. From the beginning of this chapter, we will understand the file of Qt and the function of input and output, that is I/O system.
Qt QIODevice
provides an abstraction of I/O devices that have the ability to read and write byte blocks. The following is the class diagram for I/O devices:
Qt4
Qt5
As can be seen from the class diagram above, Qt4 and QT5 are similar in the I/O devices section. Just Qt5 added a new QFileDevice
class. A brief description of the classes involved and their uses in the way is as follows:
QIODevice
: The parent class of all I/O device classes, providing common operations and basic interfaces for byte block reading and writing;
QFlie
: Accessing local files or embedding resources;
QTemporaryFile
: Create and access temporary files for the local file system;
QBuffer
: Read and write QByteArray
;
QProcess
: Run external programs to handle interprocess communication;
QAbstractSocket
: The parent class of all socket classes;
QTcpSocket:TCP
Protocol network data transmission;
QUdpSocket
: Transmitting UDP packets;
QSslSocket
: Transfer data using SSL/TLS;
QFileDevice:Qt5
The newly added class provides a generic implementation of file operations.
This is where,, QProcess
QTcpSocket
, QUdpSoctet
and QSslSocket
is sequential access to the device. The so-called "sequential access" means that their data can only be accessed once: from the beginning to the end, from the first byte to access, until the last byte, midway can not return to read the previous byte, QFile
QTemporaryFile
and QBuffer
is a random access to the device, can access any number of places, you can also use QIODevice::seek()
function to reposition the file access location pointer.
This chapter focuses QFile
on its related classes, and later chapters begin with the flow of input and output.
In all I/O devices, file I/O is one of the most important parts. Because most of our programs still need to first access local files (of course, this view may change in the future of cloud computing). QFile
provides the ability to read and write data from a file. Qt5 the newly added QFileDevice
class, this part of the public operation is placed in this separate class. Obviously, this part of the code is in the Qt4 QFile
class. In this way, QT5 's code structure is clearer and more hierarchical.
We typically pass the file path as a parameter QFile
to the constructor. However, you can also modify the object by using it at the end of the creation setFileName()
. QFile
you need to use/as a file delimiter, but it will automatically convert it into the form required by the operating system. For example, a path such as C:/windows is also available under the Windows platform.
QFile
It mainly provides various actions about files, such as opening files, closing files, refreshing files, etc. We can use the QDataStream
or QTextStream
class to read and write files, or we can use the QIODevice
functions provided by the class, and read()
readLine()
readAll()
write()
so on. It is important to note that information about the file itself, such as the filename, the name of the directory where the file resides, is QFileInfo
obtained, rather than parsing the file path string yourself.
Let's take a look at the following code QFile
:
123456789101112131415161718192021222324 |
int main(int argc, char *argv[]) { qapplication app(argc, argv); qfile file if (! File. Open (qiodevice:: Readonly | qiodevice::text) Span class= "Crayon-sy" >) { qdebug() << "Open file failed." ; return -1; } else { While (! File. AtEnd()) { qdebug() << file. ReadLine(); } } qfileinfo info(file); qdebug() << info. Isdir(); qdebug() << info. IsExecutable(); qdebug() << info. BaseName(); qdebug() << info. Completebasename(); qdebug() << info. Suffix(); qdebug() << info. Completesuffix(); return app. EXEC(); } |
In this code, we first use the QFile
creation of a file object. The name of this file is in.txt. If you don't know where to put it, you can use it app.applicationFilePath();
or app.applicationDirPath();
get the application's execution path. Simply place the file in a directory that is consistent with the execution path. can be used QDir::currentPath()
to get the current path when the application executes. Simply place the file in a directory that is consistent with the current path. Then we use the open()
function to open this file, open form is read-only, text format. This is similar to the fopen()
R parameter. The open()
function returns a bool type, and if open fails, we output a hint in the console and the program exits. Otherwise, we use the while loop to output the content read by each line.
The second part of the program, we use to QFileInfo
get information about the file. QFileInfo
There are many types of functions, and we only cite a few examples. For example, isDir()
check whether the file is a directory, isExecutable()
check whether the file is an executable, and so on. baseName()
you can get the file name directly, and suffix()
get the suffix name directly. We can see by the following example, baseName()
and completeBaseName()
, as well as suffix()
completeSuffix()
the difference between:
12345 |
qfileinfo fi("/tmp/archive.tar.gz"); QString base = fi. BaseName(); //base = "Archive" QString cbase = fi. Completebasename(); //base = "Archive.tar" QString ext = fi. Suffix(); //ext = "GZ" QString ext = fi. Completesuffix(); //ext = "tar.gz" |
Qt Learning Path: Documents