Results 1 to 8 of 8

Thread: Reading a binary file with QT

  1. #1
    Join Date
    Apr 2006
    Location
    Saint-Petersburg, Russia
    Posts
    63
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Reading a binary file with QT

    I have to read binary file via Qt. In console application I make

    Qt Code:
    1. FILE * fid5 = fopen ("./test_data/source.rgg", "rb");
    2.  
    3. ...
    4. for (int i=1; i<=na; i++)
    5. {
    6. fread (st+i-1, sizeof (unsigned long), nd2, fid5);
    7. ...
    8. }
    To copy to clipboard, switch view to plain text mode 

    and all work fine. In Qt application I try
    Qt Code:
    1. QString fileName = QFileDialog::getOpenFileName (this, tr("Select
    2. source file"), QDir::currentPath(), tr("All files (*)"));
    3. if (fileName.isEmpty())
    4. return;
    5. ...
    6.  
    7. QFile * fData = new QFile (fileName);
    8.  
    9. for (int i=1; i<=na; i++)
    10. {
    11. char * colData = new char [nd2*sizeof (unsigned long)];
    12. qint64 colLength = fData->readLine (colData, nd2*sizeof
    13. (unsigned long));
    14. if (colLength < 0)
    15. {
    16. qDebug () << __PRETTY_FUNCTION__ << QString ("Read error");
    17. continue;
    18. }
    19. QByteArray buff (colData);
    20. delete [] colData;
    21. //qDebug () << __PRETTY_FUNCTION__ << i << buff;
    22.  
    23. QBuffer lBuf (&buff);
    24. lBuf.open (QIODevice::ReadOnly);
    25. QDataStream numStr (&lBuf);
    26. quint64 num;
    27. for (int ii=0; ii<nd2; ii++)
    28. {
    29. numStr >> num;
    30. qDebug () << __PRETTY_FUNCTION__ << num;
    31. st[ii] = num;
    32. }
    33. ...
    34. }
    To copy to clipboard, switch view to plain text mode 

    And for the same file in debug numbers do not read properly.
    Thanks a lot.
    Best regards,
    Yuriy Rusinov.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reading a binary file with QT

    First of all why in Qt code You are using readLine ? This method is for reading character data not binary.

  3. #3
    Join Date
    Apr 2006
    Location
    Saint-Petersburg, Russia
    Posts
    63
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Reading a binary file with QT

    Quote Originally Posted by Lesiok View Post
    First of all why in Qt code You are using readLine ? This method is for reading character data not binary.
    I try using method read, such this
    Qt Code:
    1. char * colData = new char [nd2*sizeof (unsigned long)];
    2. qint64 colLength;// = nd2*sizeof (unsigned long);
    3. //stStream.readBytes (colData, colLength);
    4. colLength = fData.read (colData, nd2*sizeof (unsigned long));
    5. if (colLength <= 0)
    6. {
    7. qDebug () << __PRETTY_FUNCTION__ << i << QString ("Read error");
    8. return;
    9. }
    10. QByteArray buff (colData);
    11. delete [] colData;
    To copy to clipboard, switch view to plain text mode 
    but error was arises. Which way I have to trace this error ?
    Best regards,
    Yuriy Rusinov.

  4. #4
    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: Reading a binary file with QT

    You forgot to open the file.
    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.


  5. #5
    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: Reading a binary file with QT

    Once you have the file open...

    You are reading nd2 unsigned longs (probably 4 bytes each) from the file and then trying to extract nd2 unsigned 64-bit integers from that data (lines 26 and 29).

  6. #6
    Join Date
    Apr 2006
    Location
    Saint-Petersburg, Russia
    Posts
    63
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Reading a binary file with QT

    Sorry, I skip this
    Qt Code:
    1. if (!fData.open (QIODevice::ReadOnly | QIODevice::Unbuffered))
    2. return;
    To copy to clipboard, switch view to plain text mode 
    But nothing changes.
    Last edited by YuriyRusinov; 20th December 2012 at 07:54.
    Best regards,
    Yuriy Rusinov.

  7. #7
    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: Reading a binary file with QT

    What else did you skip?

    BTW. You are reading data using QDataStream. Was it written using QDataStream?
    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.


  8. #8
    Join Date
    Apr 2006
    Location
    Saint-Petersburg, Russia
    Posts
    63
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Reading a binary file with QT

    Problem was in environment. I forgot define macro _FILE_OFFSET_BITS=64 in 32 bit environment. Thanks a lot.
    Best regards,
    Yuriy Rusinov.

Similar Threads

  1. Reading string in Utf8 from binary file
    By iddqd in forum Newbie
    Replies: 3
    Last Post: 19th July 2010, 06:16
  2. Binary file reading using Structure
    By umulingu in forum Qt Programming
    Replies: 6
    Last Post: 25th July 2009, 11:35
  3. Reading/writing data to binary file
    By DiamonDogX in forum Qt Programming
    Replies: 3
    Last Post: 23rd July 2009, 19:24
  4. Reading characters in Binary file.....
    By umulingu in forum Qt Programming
    Replies: 2
    Last Post: 23rd July 2009, 04:51
  5. Binary file Reading.........
    By umulingu in forum Qt Programming
    Replies: 11
    Last Post: 20th July 2009, 06:18

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.