Results 1 to 10 of 10

Thread: Read binary file

  1. #1
    Join Date
    Dec 2007
    Location
    Brazil
    Posts
    61
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Read binary file

    Hi,
    I am with problem in the reading of a binary file.
    I made thus:

    Qt Code:
    1. #include <QApplication>
    2. #include <QDataStream>
    3. #include <QFile>
    4. #include <QVector>
    5.  
    6. #include <iostream>
    7. using namespace std;
    8.  
    9. int main(int argc, char *argv[]) {
    10.  
    11. QApplication app(argc, argv);
    12.  
    13. QFile file("wavemin_501ns.ad");
    14. file.open(QIODevice::ReadOnly);
    15. QDataStream in(&file);
    16.  
    17. qint64 tam = file.size();
    18. qint64 ns = tam/sizeof(float);
    19. QVector<float> vet(tam);
    20. in >> vet;
    21.  
    22. for (int i = 0; i < 50; i++)
    23. cout << vet[i] << " " << i << endl;
    24. cout << ns << endl;
    25.  
    26. return 0;
    27. }
    To copy to clipboard, switch view to plain text mode 

    But the values returned for vet[] are not equal to the values of binary file.
    Somebody can help me, please?
    Thank you.
    Last edited by wysota; 10th January 2008 at 23:34. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Read binary file

    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.

  3. #3
    Join Date
    Dec 2007
    Location
    Brazil
    Posts
    61
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Read binary file

    The values inside of file are all float.
    Does not exist a form in Qt to catch these values of file?

  4. #4
    Join Date
    Dec 2007
    Location
    Brazil
    Posts
    61
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Read binary file

    They see as I made. It gave certain, but it wanted to use Qt.

    Qt Code:
    1. ifstream file("wavemin_501ns.ad", ios::in | ios::binary);
    2. file.seekg (0, ios::end);
    3. int tam = file.tellg();
    4. file.seekg (0, ios::beg);
    5. qint64 ns = tam/sizeof(float);
    6. QVector<float> vet(tam);
    7. for(int i = 0; i < tam; i++)
    8. file.read(reinterpret_cast < char * > (&vet[i]), sizeof(float));
    9. file.close();
    10. for (int i = 0; i < ns; i++)
    11. cout << vet[i] << " " << i << endl;
    12. cout << tam << " " << ns << endl;
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 11th January 2008 at 10:52. Reason: missing [code] tags

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Read binary file

    Quote Originally Posted by jaca View Post
    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.

    Quote Originally Posted by jaca View Post
    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.

  6. #6
    Join Date
    Aug 2009
    Location
    Melbourne Australia
    Posts
    6
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Read binary file

    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().

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Read binary file

    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.

  8. The following user says thank you to Lykurg for this useful post:

    carllooper (28th March 2012)

  9. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Read binary file

    Quote Originally Posted by carllooper View Post
    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.

  10. The following user says thank you to ChrisW67 for this useful post:

    carllooper (28th March 2012)

  11. #9
    Join Date
    Aug 2009
    Location
    Melbourne Australia
    Posts
    6
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Read binary file

    Ok. Fair enough. DataStream is for data originating in QT datatypes rather than from some unknown source.

  12. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Read binary file

    Quote Originally Posted by carllooper View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. How can I read Binary files with Qt
    By geo_saleh in forum Qt Programming
    Replies: 2
    Last Post: 16th August 2007, 10:37
  2. How to read text only as it is from file
    By thomasjoy in forum Qt Programming
    Replies: 3
    Last Post: 9th August 2007, 08:47
  3. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21
  4. Sending Binary File with QFTP
    By nbkhwjm in forum Newbie
    Replies: 2
    Last Post: 7th March 2007, 18:10
  5. Replies: 13
    Last Post: 1st June 2006, 14:01

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.