Results 1 to 7 of 7

Thread: QDataStream>>(int) skipping one byte?

  1. #1
    Join Date
    Sep 2015
    Posts
    36
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default QDataStream>>(int) skipping one byte?

    I have a problem with reading binary file. File was written using standard C++ fwrite functions, and now I want to read it using QDataStream. Yet I have problem that I cannot explain.

    Qt Code:
    1. //dataset description
    2. fileStream>>(int)tempInt; //length of description, this is correctly loaded, int = 121
    3. fileStream.readRawData(charBuffer,tempInt); // this is correctly loaded too
    4.  
    5. qDebug() << "Dataset descr length: " << tempInt;
    6.  
    7. for (int i=0; i<tempInt; i++){
    8. dataSetDescription += charBuffer[i]; // it works too
    9. }
    10.  
    11. //signals count
    12. fileStream>>(int)signalsCount; //and it doesn't work there, it should read int=13, yet it reads 16777216
    To copy to clipboard, switch view to plain text mode 

    IT'S NOT ENDIANESS PROBLEM! One byte is simply skipped for whatever reason.

    http://s11.postimg.org/tp2xi4hz5/hex.png

    Take a look at this binary file. Whole name of dataset is loaded correctly and ended with . sign. After this . sign I want to read 4 bytes in little endian being number of signals. As you can see it should read 0D000000, which is 13. YET it totally skips 0D byte and proceeds further, and reads 00000010 which means 16777216 in little endian hex. I just have no clue what is happening here. You can see, that there is nothing in code that would order it to move this one byte further. Also same algorithm you see is used in reading many things before signals count and it skips no bytes, and all chars are converted to strings correctly. Any ideas?
    Attached Images Attached Images
    Last edited by Khaine; 15th March 2016 at 21:20.

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QDataStream>>(int) skipping one byte?

    QDataStream reads and writes in encoded format, which includes the QDataStream version to ensure compatibility when reading/writing the data. Since your data was not written using QDataStream, your issue is likely due to the format of data written using fwrite doesn't contain the exact format expected by QDataStream.

    The x'0D' following your string is likely a carriage return. From the docs:
    To take one example, a char * string is written as a 32-bit integer equal to the length of the string including the '\0' byte, followed by all the characters of the string including the '\0' byte. When reading a char * string, 4 bytes are read to create the 32-bit length value, then that many characters for the char * string including the '\0' terminator are read.
    So when you write your file using fwrite, does the 32-bit integer length include the carriage return and trailing '\0' byte in its length?

    Edit: QDataSteam also encodes a version number into the stream, which could also be where you're getting tripped up since your file wasn't written with QDataStream.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  3. #3
    Join Date
    Mar 2016
    Posts
    5
    Qt products
    Platforms
    Symbian S60

    Default Re: QDataStream>>(int) skipping one byte?

    may be if there is a option to add watch then -> add watch from the 1st statement and then compile it line by line, i am sure you will recover your problem with reading this.

    Good Luck !

  4. #4
    Join Date
    Sep 2015
    Posts
    36
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QDataStream>>(int) skipping one byte?

    And what can I do when

    Qt Code:
    1. //dataset description
    2. fileStream>>(int)tempInt; //length of description
    3. fileStream.readRawData(charBuffer,tempInt);
    4.  
    5. qDebug() << "Dataset descr length: " << tempInt;
    6.  
    7. for (int i=0; i<tempInt; i++){
    8. dataSetDescription += charBuffer[i];
    9. }
    10.  
    11. fileStream.readRawData(charBuffer,4);
    12. memcpy(&signalsCount, charBuffer, 4);
    To copy to clipboard, switch view to plain text mode 

    does the same? 0D is still missed.

    Qt Code:
    1. //dataset description
    2. fileStream>>(int)tempInt; //length of description
    3. fileStream.readRawData(charBuffer,tempInt+4);
    4.  
    5. qDebug() << "Dataset descr length: " << tempInt;
    6.  
    7. for (int i=0; i<tempInt; i++){
    8. dataSetDescription += charBuffer[i];
    9. }
    10.  
    11. memcpy(&signalsCount, charBuffer+(tempInt*sizeof(char)), 4);
    To copy to clipboard, switch view to plain text mode 

    Even this misses 0D byte. It's just impossible what is happening here.
    Last edited by Khaine; 16th March 2016 at 14:07.

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QDataStream>>(int) skipping one byte?

    You are using two incompatible methods of writing and reading.

    Just don't use QDataStream and you should be fine.

    Cheers,
    _

  6. #6
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QDataStream>>(int) skipping one byte?

    Quote Originally Posted by Khaine View Post
    It's just impossible what is happening here.
    Clearly not impossible, because it is happening! As @anda_skoa pointed out more clearly than I did, the format you're writing the data clearly isn't compatible with trying to read the data back in with QDataStream. You can keep struggling and not understanding why, or you can abandon QDataStream unless the data you're trying to read is *also* written using QDataStream.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  7. #7
    Join Date
    Sep 2015
    Posts
    36
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QDataStream>>(int) skipping one byte?

    I figured it out. QIODevice::Text was accidentally set. So it treated this sign as end line probably.

Similar Threads

  1. byte to uint
    By #Dragon in forum Qt Programming
    Replies: 2
    Last Post: 27th October 2015, 17:29
  2. QDataStream on enum (byte length)
    By StarShaper in forum Qt Programming
    Replies: 0
    Last Post: 2nd February 2012, 02:28
  3. Replies: 3
    Last Post: 19th April 2010, 15:16
  4. Phonon skipping two tracks
    By matio in forum Qt Programming
    Replies: 4
    Last Post: 24th November 2009, 18:03
  5. Byte shifting in C
    By tntcoda in forum General Programming
    Replies: 3
    Last Post: 14th November 2008, 23:40

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.