PDA

View Full Version : qt binary file writing and reading



seniorc
16th December 2013, 11:22
Hello,

void write(QString filename) {
QChar ch('b');
QFile mfile(filename);
if (!mfile.open(QFile::WriteOnly) {
qDebug() << "Could not open file for writing";
return;
}
QDataStream out(&mfile);
out.setVersion(QDataStream::Qt_4_8);
out << ch;
mfile.close();
}

open binary file and writing ‘b’(binary)

void read(QString filename) {
QFile mfile(filename);
if (!mfile.open(QFile::ReadOnly)) {
qDebug() << "Could not open file for reading";
return;
}
QDataStream in(&mfile);
in.setVersion(QDataStream::Qt_4_8);
QChar mT;
in >> mT;
qDebug() << mT;
mfile.close();
}

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;

int x=4;
QString abc="hello";
char ch ='a';

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

wysota
16th December 2013, 13:23
Do you understand what QDataStream does?

seniorc
16th December 2013, 19:15
problem is here.Yes i try understand QDataStream.but you see i cant this.Whats my problem ?

anda_skoa
16th December 2013, 19:30
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,
_

seniorc
17th December 2013, 01:27
im working huffman encoding.I have QMap and QString(character code) and i must writing them binary file but icant this.Whats way this?

ChrisW67
17th December 2013, 04:22
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

seniorc
17th December 2013, 21:13
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.


QMap<char,int>mymap;
//char -----> character from table
//int -----> frequency from the table
//so QMap keeping character and frequency i mean QMap my table.
Now for example my text is "aabkdgmma" so this codes(look table) are "0 0 10 110 11111 11110 1110 1110 0"->
--->"00101101111111110111011100"

QString codes;
//codes="00101101111111110111011100" (they are bits).
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.

wysota
17th December 2013, 21:54
Have a look at QByteArray and QFile::write(). That's all you need.

ChrisW67
17th December 2013, 22:29
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/materials/HyperGraph/video/mpeg/mpegfaq/huffman_tutorial.html

seniorc
17th December 2013, 23:03
Since...

Thanks to you I found an easier method I can use in my project.