File stream and data stream, file stream data stream
1. File Type
1.1,In Qt, file types are dividedTwo categories
1.1.1,Text file:The file content is a readable text character
1.1.2,Data file:The file content is binary data directly.
1.2,QFileDirectly supporting text files and data filesRead/write
. Cpp File
# Include
# Include
# Include
# Include
Int main (int argc, char * argv [])
{
QCoreApplication a (argc, argv );
QFile file ("H:/BaiduNetdiskDownload/chuanzhibokec ++/test. hex"); // binary file
If (file. open (QIODevice: WriteOnly) // if QIODevice: Text is not added, it is opened as a data stream instead of Text.
{
QString name = "MyNameLiYuan ";
Double value = 3.14;
Std: string t = name. toStdString (); // avoid generating temporary objects.
Const char * tem = t. c_str ();
// Type conversion is required when writing data in binary format, which is also a troublesome issue in QFile usage.
File. write (tem );
File. write (reinterpret_cast (& Value), sizeof (value); // It must be of the const char * type.
File. close ();
// QDebug () <value;
}
If (file. open (QIODevice: ReadOnly ))
{
QString name = "";
Double value = 0;
// The returned value of read is a QByteArray byte array. When the value is assigned to the QString variable type, the data type is automatically converted. The following statement can also be written as name = file. read (10 );
Name = QString (file. read (10 ));
File. read (reinterpret_cast (& Value), sizeof (value ));
File. close ();
QDebug () <name;
QDebug () <value; // garbled value.
}
Return a.exe c ();
}
Ii. Text Stream and Data Stream
2.1,Qt providesAuxiliary classSimplifiedRead and Write of text files/data files
2.1.1,QTextStream: converts all written data to readable text.
2.1.2,QDataStream: the written data is converted to binary by type
2.2,How to Use the I/O device helper class
(Create an IO device object)
2.3,Notes
2.3.1,Different Qt versions may have different data stream formats.
2.3.2,When the data stream text may be transmitted between different versions of Qt programs, you need to consider the version issue.
# Include
# Include
# Include
# Include
// Text stream
Void textstream (QString f)
{
/************ 1. Create an IO device object *************/
QFile file (f );
/*********** 2. Use a file object to open a file **********/
If (file. open (QIODevice: WriteOnly | QIODevice: Text) // open the file in Text format.
{
/*********** 3. Write data to a file ******************/
QTextStream out (& file );
Out <QString ("DTSoftWare") <endl;
Out <3.14 <endl;
File. close ();
}
If (file. open (QIODevice: ReadOnly | QIODevice: Text ))
{
// 4. read data from the file
QTextStream in (& file );
While (! In. atEnd () // if you have not finished reading atEnd, false is returned.
{
QDebug () <in. readLine (); // read content row by row.
}
File. close ();
}
}
// Data stream
Void dataStream (QString f)
{
// 1. Create an I/O device object QFile object file
QFile file (f );
// 2. Open a file using a file object
If (file. open (QIODevice: WriteOnly) // open it in binary-only mode.
{
// 3. Write data to a file
QDataStream out (& file );
Out. setVersion (QDataStream: Qt_4_7); // you can specify the version number to transfer data on Qt of different versions.
Out <QString ("d t soffffteware ");
Out <3.15;
File. close ();
}
If (file. open (QIODevice: ReadOnly ))
{
// 4. read data from the file
QDataStream in (& file );
QString s = "";
Double d = 0;
In. setVersion (QDataStream: Qt_4_7); // you can specify the version number to transfer data to different versions of Qt.
In> s;
In> d;
File. close ();
QDebug () <s;
QDebug () <d; // use the helper class to perform operations without garbled characters.
}
}
Int main (int argc, char * argv [])
{
QCoreApplication a (argc, argv );
// QFile file ("H:/BaiduNetdiskDownload/chuanzhibokec ++/test. hex"); // binary file
Textstream ("H:/BaiduNetdiskDownload/chuanzhibokec ++/test1.hex ");
DataStream ("H:/BaiduNetdiskDownload/chuanzhibokec ++/test1.hex ");
Return a.exe c ();
}
3. Summary
3.1,Qt filesAuxiliary classUsed to facilitate read/write operations
3.2,QTextStreamUsedText DataFast read/write
3.3,QDataStreamUsedBinary dataFast read/write
3.4,QDataStreamFile FormatRelated to Qt
3.5,Data Format FileDuring Inter-program transmission,Version considerations