Results 1 to 3 of 3

Thread: writing and reading problem

  1. #1
    Join Date
    Dec 2013
    Posts
    20
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default writing and reading problem

    Hello,writing codes here
    Qt Code:
    1. //Writing size of map
    2. uint16_t streamOffset = (sizeof(int)+sizeof(char))*FrekMap.size()+sizeof(uint16_t);
    3. codefile.write(reinterpret_cast<char*>(&streamOffset),sizeof(uint16_t));
    4. //Writing map
    5. for (QMap<char,int>::Iterator it = FrekMap.begin(); it != FrekMap.end(); ++it) {
    6. codefile.putChar(it.key());
    7. codefile.write(reinterpret_cast<char*>(&(it.value())), sizeof(int));
    8. }
    9. //Writing lenght last byte
    10. codefile.putChar(compressed.length()%8);
    11. while(compressed.length()%8!=0)
    12. compressed.push_back('0');
    13. //Writing code
    14. compressedData=compressed.constData();
    15. for(int i = 0; i < compressed.size(); i++ )
    16. {
    17. if( compressedData[i] == ZeroBit )
    18. k++;
    19. else if( compressedData[i] == OneBit )
    20. {
    21. buff |= bits[k];
    22. k++;
    23. }
    24. if( k == 8 )
    25. {
    26. codefile.write(reinterpret_cast<char *>(&buff),sizeof(unsigned char));
    27. buff = 0;
    28. k = 0;
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 
    Reading codes here
    Qt Code:
    1. //read size of map
    2. readData.read(reinterpret_cast<char*>(&sizeMap),sizeof(sizeMap));
    3. //read map
    4. for (int i=sizeof(uint16_t);i<sizeMap;i+=sizeof(int)+sizeof(char)) {
    5. char charofmap;
    6. int freq;
    7. readData.getChar(reinterpret_cast<char*>(&charofmap));
    8. readData.read(reinterpret_cast<char*>(&freq), sizeof(int));
    9. FreqTable.insert(charofmap,freq);
    10. }
    11. //read last byte lenght
    12. readData.getChar(reinterpret_cast<char*>(&lengthLast));
    13. //read code to bit
    14. while (!readData.atEnd()) {
    15. unsigned char ch;
    16. readData.getChar(reinterpret_cast<char*>(&ch));
    17. codeLetter+=unschartoQString(ch);
    18. }
    To copy to clipboard, switch view to plain text mode 

    I can't read true for writing code.Where is my problem?

  2. #2
    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: writing and reading problem

    I would suggest to test the invidual pieces.
    First check that the size is written and read correctly, because there is no point in continuing if it is not.

    Then check writing and reading a map with one entry, and so on.

    Btw, why such a complicated way of writing the map size? If you just write the map size's value you can also more easily iterate over that range when reading, no?

    Cheers,
    _

  3. #3
    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: writing and reading problem

    I can't read true for writing code.Where is my problem?
    You have failed to explain what you are expecting to see, what you are seeing, why that is wrong, what you done to diagnose it etc.

Similar Threads

  1. Help in reading and writing xml using qt
    By shah_27 in forum Qt Programming
    Replies: 2
    Last Post: 5th January 2013, 08:32
  2. QTcpSocket writing reading problem
    By Misenko in forum Qt Programming
    Replies: 4
    Last Post: 15th October 2008, 07:27
  3. Reading/writing QTableWidget
    By Salazaar in forum Newbie
    Replies: 15
    Last Post: 23rd June 2007, 13:20
  4. Function for reading and writing
    By merry in forum Newbie
    Replies: 2
    Last Post: 30th May 2007, 08:30
  5. reading/writing to file
    By QiT in forum Newbie
    Replies: 2
    Last Post: 11th August 2006, 17:21

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.