PDA

View Full Version : Read binary file



jaca
10th January 2008, 23:22
Hi,
I am with problem in the reading of a binary file.
I made thus:


#include <QApplication>
#include <QDataStream>
#include <QFile>
#include <QVector>

#include <iostream>
using namespace std;

int main(int argc, char *argv[]) {

QApplication app(argc, argv);

QFile file("wavemin_501ns.ad");
file.open(QIODevice::ReadOnly);
QDataStream in(&file);

qint64 tam = file.size();
qint64 ns = tam/sizeof(float);
QVector<float> vet(tam);
in >> vet;

for (int i = 0; i < 50; i++)
cout << vet[i] << " " << i << endl;
cout << ns << endl;

return 0;
}

But the values returned for vet[] are not equal to the values of binary file.
Somebody can help me, please?
Thank you.

wysota
10th January 2008, 23:36
Don't use QDataStream unless you are sure you know what you are doing. Use QFile's QIODevice API instead.

QDataStream is a serialization mechanism, not a general purpose binary stream. If you want one, use std::fstream.

jaca
10th January 2008, 23:49
The values inside of file are all float.
Does not exist a form in Qt to catch these values of file?

jaca
11th January 2008, 01:06
They see as I made. It gave certain, but it wanted to use Qt.


ifstream file("wavemin_501ns.ad", ios::in | ios::binary);
file.seekg (0, ios::end);
int tam = file.tellg();
file.seekg (0, ios::beg);
qint64 ns = tam/sizeof(float);
QVector<float> vet(tam);
for(int i = 0; i < tam; i++)
file.read(reinterpret_cast < char * > (&vet[i]), sizeof(float));
file.close();
for (int i = 0; i < ns; i++)
cout << vet[i] << " " << i << endl;
cout << tam << " " << ns << endl;

wysota
11th January 2008, 10:52
The values inside of file are all float.
Oh, very nice, but float in which endianness? Of what length? QDataStream needs to take that all into consideration thus it stores additional information in the stream and expects that information to be present when reading the stream.


Does not exist a form in Qt to catch these values of file?
What for if std::fstream already does that? If you keep an unportable data in a file, you need an unportable solution to it. QDataStream is not one. Use QIODevice::read() and cast or convert the output into the format you know is suitable.


They see as I made. It gave certain, but it wanted to use Qt.
Qt is not a panaceum. It is meant to provide portable solutions and yours is not portable thus it is not supported by Qt.

carllooper
28th March 2012, 05:51
I dunno. From the docs it seems to suggest otherwise:

Data is usually read and written using QDataStream or QTextStream, but you can also call the QIODevice-inherited functions read(), readLine(), readAll(), write().

Lykurg
28th March 2012, 06:10
First of all: The post is 4 (four) years old. Qt has developed since then, and the point is that the file was most likely not written with QDataStream. Thus it could not be read in again using QDataStream. Thats all. If you write and read the same file again, then of course use QDataStream.

ChrisW67
28th March 2012, 07:24
I dunno. From the docs it seems to suggest otherwise:

Data is usually read and written using QDataStream or QTextStream, but you can also call the QIODevice-inherited functions read(), readLine(), readAll(), write().

Your unreferenced quote from the QFile docs specifically tells you "usually". The majority of files read by Qt applications will be text files from any source (QTextStream), or binary files written by the Qt application itself (QDataStream). Thus "usually" covers the majority of cases. The same sentence gives you options for dealing with everything else, i.e. arbitrary binary data. You can, if you wish, use QDataStream to read arbitrary binary data as the docs say:


You can also use a data stream to read/write raw unencoded binary data. If you want a "parsing" input stream, see QTextStream.

and


Reading and writing raw binary data

You may wish to read/write your own raw binary data to/from the data stream directly. Data may be read from the stream into a preallocated char * using readRawData(). Similarly data can be written to the stream using writeRawData(). Note that any encoding/decoding of the data must be done by you.

This is, however, not the usual mode of use and you might as well use the raw QIODevice mechanisms.

The OP's issue four years ago was of understanding. A QVector<float>(10) when serialised to a QDataStream (expect 44 bytes) is not the same thing as the same 10 floats written raw to a file (expect 40 bytes). Consequently streaming from a QDataStream into a QVector<float> from a file of 10 raw floats was not going to work. It would probably, in fact, behave very badly.

carllooper
28th March 2012, 08:00
Ok. Fair enough. DataStream is for data originating in QT datatypes rather than from some unknown source.

wysota
28th March 2012, 08:38
DataStream is for data originating in QT datatypes rather than from some unknown source.
No, that's not true. Reading using QDataStream is meant for data which was written using QDataStream. Writing with QDataStream is for data going to be read with QDataStream. Regardless of the data type itself. QDataStream is a serialization mechanism and not a general purpose binary stream.