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 provides an abstraction of I/O devices through Qiodevice, which 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. But Qt5 added a new Qfiledevice class. A brief description of the classes involved and their uses in the way is as follows:
class name |
action |
qiodevice |
The parent class of all I/O device classes, provides common operations for byte block read and write, and basic interface |
qflie |
Access this Files or embedded resources |
qtemporaryfile |
Create and access temporary files on the local file system |
qbuffer |
read-write Qbytearray |
qprocess |
run an external program to process the Inter-process Communication |
qabstractsocket |
parent class for all socket classes |
qtcpsocket |
TCP Protocol Network data transfer |
qudpsocket |
transmitting UDP messages |
qsslsocket |
|
qfiledevice | TD align= "Center" >qt5 newly added class, provides a generic implementation of file operations
Among these, qprocess, Qtcpsocket, Qudpsoctet, and qsslsocket are sequential access devices. 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 are random access devices, You can access any number of times in any location, and you can use the Qiodevice::seek () function to reposition the file access location pointer.
This chapter focuses on Qfile and 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 Qfile class in Qt4. In this way, QT5 's code structure is clearer and more hierarchical.
We usually pass the file path as an argument to the Qfile constructor. However, you can also use Setfilename () to modify it at the end of creating a good object. Qfile needs to use/as a file delimiter, but it automatically translates 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 mainly provides various operations related to files, such as opening files, closing files, refreshing files, and so on. We can use the Qdatastream or Qtextstream classes to read and write files, or you can use the Read (), ReadLine (), ReadAll (), and write () functions provided by the Qiodevice class. 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 obtained through qfileinfo, rather than parsing the file path string yourself.
Let's take a look at some of the Qfile's operations using a piece of code:
int main (int argc, Char*argv[]) {qapplication app (argc, argv); QFilefile ("In.txt"); if (! File.Open (Qiodevice::readonly | Qiodevice::text) {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 piece of code, we first created a file object using Qfile. 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 while
loops to output the content that each line reads.
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:
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"
http://blog.csdn.net/u013007900/article/details/46459535
QT Learning Documents