1. Qt mid-io operation
(1) How to handle the IO operation in QT
①QT simplifies the operation of files and external devices through a unified interface
files in ②QT are treated as a special kind of external device
The file operation in ③QT is the same as the operation of an external device
(2) Key function interface in IO operation--the essence of IO operation : data reading and writing of continuous storage space
① Open Device: BOOL Open (OpenMode mode);
② Read data: Qbytearray read (Qint64 maxSize);
③ Write Data: Qint64 write (const qbytearray& byteArray);
④ shutdown device: void close ();
(3) Type of IO device in QT
① Sequential storage device : Read and write data only from the beginning , and cannot specify the read/write location of the data (e.g. serial port)
② Random Access device : Can be located to any location to read and write data (Seek function), such as file
2. The inheritance hierarchy of IO devices in QT
3. File manipulation in QT
(1)QFile is the class used for file manipulation in QT
(2) The Qfile object corresponds to a file on the computer
(3)Qfileinfo class for reading property information of a file
Initial experience of "programming experiment" file operation
32-1.pro
QT + = = + C + +-1 + = = = + = Main.cpp
Main.cpp
#include <QCoreApplication>#include<QFile>#include<QFileInfo>#include<QDateTime>#include<QDebug>voidwrite (QString f) {QFile file (f); //Writable , text text that creates a new file when the file does not exist if(File.Open (Qiodevice::writeonly |Qiodevice::text)) {File.write ("line1\n"); File.write ("line2\n"); File.close (); }}voidRead (QString f) {QFile file (f); if(File.Open (Qiodevice::readonly |Qiodevice::text)) {Qbytearray ba= File.readall ();//File.readline (5);//Read 4 charactersQString s (BA); Qdebug ()<<s; File.close (); }}voidinfo (QString f) {QFile file (f); Qfileinfo info (file); Qdebug ()<<info.exists (); Qdebug ()<< Info.isfile ();//determine a file or folderQdebug () <<info.isreadable (); Qdebug ()<<info.iswritable (); Qdebug ()<<info.created (); Qdebug ()<<Info.lastread (); Qdebug ()<<info.lastmodified (); Qdebug ()<< Info.path ();//path, not including file nameQdebug () << info.filename ();//such as "Test.txt"Qdebug () << info.suffix ();//suffix name (e.g. txt)Qdebug () <<info.size ();}intMainintargcChar*argv[]) {Qcoreapplication A (argc, argv); QString file ("C:/users/santaclaus/desktop/test.txt"); Write (file); Read (file); info (file); returna.exec ();}
4. temporary file operation in QT class:qtemporaryfile
(1) Securely create a globally unique temporary file (stored in the temporary file directory of the system)
(2) When the object is destroyed , the corresponding temporary file will be deleted .
(3) The Open mode of temporary file has been specified as:qiodevic::readwrite
(4) Temporary files are inherited from Qfile, and are commonly used in large data transmission or interprocess communication situations.
Use of the "programming experiment" temp file
32-2.pro
QT + = = + C + +-2 + = = = + = Main.cpp
View Code
Main.cpp
#include <QCoreApplication>#include<QTemporaryFile>#include<QFileInfo>#include<QDebug>intMainintargcChar*argv[]) {Qcoreapplication A (argc, argv); Qtemporaryfile Tempfile; //no path specified (saved in the system Temp folder)//The file name does not have to be specified, it is a temporary file name if(Tempfile.open ()) {Tempfile.write ("Santaclaus"); Tempfile.close (); } tempfile.open (); Qbytearray BA=Tempfile.readall (); QString s (BA); Qdebug ()<<s; Qfileinfo info (tempfile); Qdebug ()<<Info.isfile (); Qdebug ()<<Info.path (); Qdebug ()<<Info.filename (); returna.exec ();}/*output: "Santaclaus" true "C:/users/santaclaus/appdata/local/temp" "32-2.hp7352"*/
5. Summary
(1) Qt reads and writes files and external devices in a unified manner
(2) The QT IO device type is divided into sequential access and random access two kinds
(3) Qfile provides methods related to file Operation
(4) Qfileinfo provides methods to read file attributes
(5) Temporary files are available in QT operation class : Qtemporaryfile
32nd lesson file manipulation in QT