Results 1 to 10 of 10

Thread: qt binary file writing and reading

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

    Question qt binary file writing and reading

    Hello,
    Qt Code:
    1. void write(QString filename) {
    2. QChar ch('b');
    3. QFile mfile(filename);
    4. if (!mfile.open(QFile::WriteOnly) {
    5. qDebug() << "Could not open file for writing";
    6. return;
    7. }
    8. QDataStream out(&mfile);
    9. out.setVersion(QDataStream::Qt_4_8);
    10. out << ch;
    11. mfile.close();
    12. }
    To copy to clipboard, switch view to plain text mode 

    open binary file and writing ‘b’(binary)
    Qt Code:
    1. void read(QString filename) {
    2. QFile mfile(filename);
    3. if (!mfile.open(QFile::ReadOnly)) {
    4. qDebug() << "Could not open file for reading";
    5. return;
    6. }
    7. QDataStream in(&mfile);
    8. in.setVersion(QDataStream::Qt_4_8);
    9. QChar mT;
    10. in >> mT;
    11. qDebug() << mT;
    12. mfile.close();
    13. }
    To copy to clipboard, switch view to plain text mode 

    read but not mT=‘b’.if ch and mT variables are int always mT=4 why?How can i writing ch(binary file) and read from binary file
    im working huffman encode project.I have all i needs(freq table,char code…)but i was writing code i cant earning size.I think this problem reason is opened binary and writing problem.How can i opened binary file and writing variables bytes.

    for example;
    Qt Code:
    1. int x=4;
    2. QString abc="hello";
    3. char ch ='a';
    To copy to clipboard, switch view to plain text mode 

    How can i writing them file.Thanks for all helping now.

  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: qt binary file writing and reading

    Do you understand what QDataStream does?
    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
    Dec 2013
    Posts
    20
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qt binary file writing and reading

    problem is here.Yes i try understand QDataStream.but you see i cant this.Whats my problem ?

  4. #4
    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: qt binary file writing and reading

    Quote Originally Posted by seniorc View Post
    problem is here.Yes i try understand QDataStream.but you see i cant this.Whats my problem ?
    wysota was asking this because there is a discrepancy between what you write about your goals and how QDataStream works.

    In the text of your posting you write that you want to do arbitrary binary I/O, which QDataStream is not intended for.

    So in order to help you further we will need to know which of the two things you actually want.

    Cheers,
    _

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

    Default Re: qt binary file writing and reading

    im working huffman encoding.I have QMap and QString(character code) and i must writing them binary file but icant this.Whats way this?

  6. #6
    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: qt binary file writing and reading

    Huffman coding for compression typically requires direct access to bytes, manipulating bits within them, and building a frequency weighted tree. What a QMap or QString have to do with that I have no idea.

    You read bytes from a QFile (QIODevice) with QIODevice::read(), and write them with QIODevice::write().

    Edit: In case there is any doubt, a QChar is not a byte

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

    Default Re: qt binary file writing and reading

    example from my project;

    frequency____character____huffman code(bit)
    -------------------------------------------------------------
    50_____________a______________0
    35_____________b______________10
    20_____________k______________110
    10_____________m_____________ 1110
    8______________d______________11111
    4______________g______________11110
    This is my freq and huff code table.I have all i needs.Now i have QMap.

    Qt Code:
    1. QMap<char,int>mymap;
    2. //char -----> character from table
    3. //int -----> frequency from the table
    4. //so QMap keeping character and frequency i mean QMap my table.
    To copy to clipboard, switch view to plain text mode 
    Now for example my text is "aabkdgmma" so this codes(look table) are "0 0 10 110 11111 11110 1110 1110 0"->
    --->"00101101111111110111011100"
    Qt Code:
    1. QString codes;
    2. //codes="00101101111111110111011100" (they are bits).
    To copy to clipboard, switch view to plain text mode 
    I want to do saving mymap and codes to file and earning size.I hope to explain my problem.How can i my bits to byte and mymap write to binary file.

    Thanks for all helping and your patience.

  8. #8
    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: qt binary file writing and reading

    Have a look at QByteArray and QFile::write(). That's all you need.
    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.


  9. The following user says thank you to wysota for this useful post:

    seniorc (17th December 2013)

  10. #9
    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: qt binary file writing and reading

    Since you decided to work with strings rather than bits you need to convert your code string into a packed set of bytes containing those bits. You need to determine how to deal with the incomplete byte you are likely to have at the end (perhaps pad with zeroes and record the number of padding bits as the first three bits of the output). The order of bit packing in bytes is important if your code is not the decoder. Then write the resulting bytes to file with QIODevice::write().

    A compact way to represent the dictionary is more complex, and you should try to walk before you run.
    http://www.siggraph.org/education/ma..._tutorial.html

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

    seniorc (17th December 2013)

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

    Default Re: qt binary file writing and reading

    Quote Originally Posted by ChrisW67 View Post
    Since...
    Thanks to you I found an easier method I can use in my project.

Similar Threads

  1. Writing my own object to a binary file.
    By 8Observer8 in forum Newbie
    Replies: 1
    Last Post: 1st December 2013, 08:15
  2. Reading a binary file with QT
    By YuriyRusinov in forum Qt Programming
    Replies: 7
    Last Post: 21st December 2012, 07:14
  3. Reading/writing data to binary file
    By DiamonDogX in forum Qt Programming
    Replies: 3
    Last Post: 23rd July 2009, 19:24
  4. Binary file Reading.........
    By umulingu in forum Qt Programming
    Replies: 11
    Last Post: 20th July 2009, 06:18
  5. reading/writing to file
    By QiT in forum Newbie
    Replies: 2
    Last Post: 11th August 2006, 17:21

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.