QT study notes QDataStream class

For QDataStream, initially through qt to read the file, and then downloaded to another device. After QDataStream provided a file by directly reading the binary data flow mode, after reading can be written elsewhere, will be sent to the analytical data. Here we must note that read out the binary files without any coding, in fact, did not encoded, are they not process data type conversion. If you read the files in this way, in order to make people read content must be converted into other types, including: char, short, int, char *, etc.

Let's look at how to read the file by QDataStream,

    QFile aFile("file.dat");  //定义一个文件,file.dat
    char* temData = new char[200];
    if (!(aFile.open(QIODevice::ReadOnly)))//用只读的方式打开文件
        return false;
    QDataStream aStream(&aFile); //定义一个数据流,指向file.dat
    aStream.readRawData(temData, 200);//读取数据,存入temData

In fact, there is a method of reading is read directly from the stream inside out, tutorials, reference beans of God, could write:

    QFile aFile("file.dat");  //定义一个文件,file.dat
    QString str;
    qint32 tem;
    if (!(aFile.open(QIODevice::ReadOnly)))//用只读的方式打开文件
        return false;
    QDataStream aStream(&aFile); //定义一个数据流,指向file.dat
    aStream >> tem >> str;//读取数据,存入tem和str

So there is a problem writing data sequentially read from the data stream is required, the order must be written, assuming the file you not read what you write, you do not know what's inside, this way it is not feasible. Because if the wrong order, it could lead to the collapse of the program directly. In this context, if you have to want to read this, then the file is best to write your own, or clearly know the order of data written. We give an example, if you want to read so that the above method is feasible, it is necessary to adopt the following method for writing:

    QFile aFile("file.dat");
    if (!(aFile.open(QIODevice::WriteOnly)))//用只写的方式打开文件
        return false;
    QDataStream aStream(&aFile);
    bStream << qint32(3);
    bStream << QString("apples");

Analyze the above order of writing and reading. When writing, data are first written qint32 shaping, rewriting QString string; a read, the data is first read TEM shaping, re-read the string str. Write and read is the corresponding sequence. However, if readRowData function to read, there is no requirement to write sequence, even if the file is foreign, wherein the order is not clear, it can be read.

Another problem is that different QT versions may also cause problems, so the data stream here, you can specify the QT version of the data stream, we give an example:

    QFile aFile("file.dat");
    if (!(aFile.open(QIODevice::WriteOnly)))//用只写的方式打开文件
        return false;   
    out.setVersion(QDataStream::Qt_5_5);//指定QT版本号
    out << QString("magic Num");    // 写入数据

In fact, the binary file is to look at the machine, the user can not distinguish between true and false, do not know whether it is wrong, so, binary files usually use magic number to mark file types, so users can determine whether the file legal , whether the right version. Magic number can be written in the beginning of the file, and a method of writing data is the same as above.

The last question, the QDataStream cursor position can be controlled by read and write, this is equivalent to a cursor pointer, it refers to where to read and write operations on its back. Imagine if we first write data, close the file or refresh the file, and then they read, will read out it? the answer is negative. Because After writing the data, the cursor is at the very end of the file, this time to read the file, the cursor back nothing. In fact, the "<<" has been redefined, so that the correct action is this:

    QFile aFile("file.dat");
    QString str;
    qint32 tem;
    if (!(aFile.open(QIODevice::WriteOnly)))//用只写的方式打开文件
        return false;   
    QDataStream aStream(&aFile);
    bStream << qint32(3); //写入数据
    bStream << QString("apples");
    bStream.device()->seek(0);//游标回到文件最开始的地方,必须有这一步!
    aFile.fush(); //这一步是刷新文件,让写进去的文件生效
    //aFile.close();
    aStream >> tem >> str;//读取数据,存入tem和str  
    qDebug() << str << tem;//打印出来看看结果

The above code has a refresh file is flush, in fact, if not this step, reading data will be empty, since write operations do not take effect. You have to refresh, refresh or not directly close the file, which is blocked phrase "aFile.close ()", so that the write operation can take effect.

Pretty much that.

Published 11 original articles · won praise 9 · views 676

Guess you like

Origin blog.csdn.net/yimuta9538/article/details/103703249