Results 1 to 7 of 7

Thread: Write QBitArray to file with QDataStream

  1. #1
    Join Date
    Sep 2011
    Posts
    3

    Default Write QBitArray to file with QDataStream

    Hi, I'm trying to write a QBitArray to file using a QDataStream but I think I may have the syntax wrong.

    This is an example of how I'm doing it.

    Qt Code:
    1. QBitArray test1;
    2. test1.resize(test1.size()+ 4);
    3. test1.setBit(0, true);
    4. test1.setBit(1, false);
    5. test1.setBit(2, true);
    6. test1.setBit(3, false);
    7.  
    8. QFile file ("testFile.data");
    9. file.open(QIODevice::WriteOnly);
    10.  
    11. QDataStream out(&file);
    12.  
    13. out << test1;
    14.  
    15. file.close();
    To copy to clipboard, switch view to plain text mode 
    Last edited by vdsf; 9th September 2011 at 13:52.

  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: Write QBitArray to file with QDataStream

    I don't know what you mean that you want to "see binary" ("Matrix"?). But since you are using QDataStream you will probably get something else than you want.
    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.


  3. #3
    Join Date
    Sep 2011
    Posts
    3

    Default Re: Write QBitArray to file with QDataStream

    Ok maybe I should elaborate a bit more. I'm writing a huffman encoding program to compress a text file. The problem I'm having is that my encoded files are coming out bigger than the original text input file, which led me to believe that my syntax was wrong.

    Also I think I took the term binary file too literally which is why I expected to see 0's and 1's when I opened it.

    Here is the code for my encoding function.

    Qt Code:
    1. void MainWindow::huffEncode(QMap<QChar, QBitArray> codeMap)
    2. {
    3. QFile file("file1.huff");
    4. file.open(QIODevice::WriteOnly);
    5. QDataStream out (&file);
    6.  
    7. for (int i = 0; i < usrText.length(); i++) // Iterate over user input string
    8. {
    9. QChar symbol = usrText[i]; // each symbol in string
    10.  
    11. QMap<QChar, QBitArray>::iterator j;
    12. for (j = codeMap.begin(); j != codeMap.end(); j++) // iterate over codeMap (QMap of characters to huffman codes)
    13. {
    14. // comparison?
    15.  
    16. if (symbol == j.key()) // if the symbol is equal to the key of the codeMap
    17. {
    18.  
    19. out << j.value(); //add the huffman code of the character to the file
    20.  
    21. }
    22.  
    23. }
    24.  
    25. }
    26. file.close();
    27.  
    28. }
    To copy to clipboard, switch view to plain text mode 

  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: Write QBitArray to file with QDataStream

    But why are you using QDataStream? Do you know what it does? It is not a transparent general purpose binary streamer but rather a serialization mechanism. Furthermore, you can't write incomplete bytes into a file. There is no way to write four bits into a file, you need to write a whole byte, no filesystem I know of allows files with sizes of fractions of a byte.
    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
    Sep 2011
    Posts
    3

    Default Re: Write QBitArray to file with QDataStream

    The reason I'm using it is because my lecturer told me to. He also said that QDataStream would automatically pad bits for QBitArray in order to make up a whole byte.

  6. #6
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Write QBitArray to file with QDataStream

    To write "raw" data to a QDataStream you must use "writeRawData". Otherwise, the data written is only readable by another instance of QDataStream.

  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: Write QBitArray to file with QDataStream

    Quote Originally Posted by vdsf View Post
    The reason I'm using it is because my lecturer told me to.
    Ask him why he did that then. It's not a correct approach.

    He also said that QDataStream would automatically pad bits for QBitArray in order to make up a whole byte.
    Doesn't it defeat the purpose of using huffman code? Besides, your lecturer is wrong.

    Qt Code:
    1. QDataStream &operator<<(QDataStream &out, const QBitArray &ba)
    2. {
    3. quint32 len = ba.size();
    4. out << len;
    5. if (len > 0)
    6. out.writeRawData(ba.d.constData() + 1, ba.d.size() - 1);
    7. return out;
    8. }
    To copy to clipboard, switch view to plain text mode 
    If you write a four bit array to data stream, 40 bits will be written to the device. And no padding is there, actually. The superflous bits are just ignored, from what I see.
    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. Empty QBitArray problem
    By MarkoSan in forum Qt Programming
    Replies: 1
    Last Post: 21st March 2011, 16:06
  2. Replies: 2
    Last Post: 2nd November 2010, 06:15
  3. Replies: 2
    Last Post: 30th June 2010, 11:48
  4. QFile - QDataStream read and write each character
    By nhs_0702 in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2010, 20:03
  5. QBitArray (the smallest unit of them all)
    By baray98 in forum Qt Programming
    Replies: 3
    Last Post: 29th September 2007, 15:16

Tags for this Thread

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.