PDA

View Full Version : can`t read a binary data!



blm
18th September 2008, 13:16
the binary file is generated from another software,
it includes text and data, a mixed binary file,

and i want use
file.open(QIODevice::Readonly)
to read it, but unsuccessfully,

can QFile only read the binary file which generated from QFile?:confused:

thanks for your HELP!

caduel
18th September 2008, 13:43
What error cause does QFile::errorString() give?
(On linux: make sure you have read permissions and, of course, that the path to the file is correct.)

blm
18th September 2008, 14:27
What error cause does QFile::errorString() give?
(On linux: make sure you have read permissions and, of course, that the path to the file is correct.)
i have tested,


if (file.open(QIODevice::Readonly))
{
QDataSteam in(&file);
QString inin;
in >> inin;
textEdit->setPlainText(inin);
}

else
cerr<<file.error();
QMessageBox::informaion(this, tr("OktoberFest"), tr("drink beer"), QMessageBox::ok);

the feedback is 5, that means The file could not be opened.

but actually i can open it use Textpad:(

caduel
18th September 2008, 15:30
print errorString() as well

blm
18th September 2008, 16:01
where can i use the errorstring()? haven`t found,

now i can read the File successfully, but not output

the File is consist of a section text date and then a section binary data,

i don`t know how i can do it !:(

caduel
18th September 2008, 16:46
errorString() is a method of QFile's base class QIOStream.


What do you mean a section of text and binary?
Using QDataStream you will read binary data.

Your code

QDataSteam in(&file);
QString inin;
in >> inin;
will not read some ascii from a (text) file, but rather the serialized binary representation of a QString which is something entirely different.
If you read ascii text with that code, interesting but unintended things will happen.

blm
18th September 2008, 17:19
thanks,
I means that this File is consist of a section of Text and then a section of binay raw date.

i can open this file with TextPad, but i can just only discern the head section (Text data), the latter is the binay raw data and i can`t discern them

.

I rewrite my code,


if (file.open(QIODevice::Readonly))
{
cerr<<file.error();
QDataSteam in(&file);
data = new char;
k = 1000;
in.readRawData(data, k);
cerr<<data;
QString inin(data);
textEdit->setPlainText(inin);
delete data;
}


i can see the feedback 0, then no more output:confused:

caduel
18th September 2008, 17:37
you must use have (or find) some way to find out where the binary part starts.
You can read that with QDataStream (if it was written by it).




// untested and uncompiled
if (file.open(QIODevice::Readonly))
{
QTextStream textinput(&file);
do {
QString line = textinput.readLine();
if (line.isNull() && textinput.atEnd()) { cerr << "done\n"; break; }
cerr << "read: " << qPrintable(line) << std::endl;
if (line == "BINARY_FROM_NOW_ON") break; // this is assuming you have a line containing this text to mark the start of the binary part of your file
}
QDataSteam in(&file);
// read binary stuff here
}


Also, note that your code is buggy:
you alloce a single byte (in; data = new char;) and then tell Qt to read a 1000 bytes into that "buffer".
Apart from the fact that dynamic memory allocation is not needed here.
try

char buffer[1000];
in.readRawData(buffer, 1000);

blm
18th September 2008, 17:56
thank u very much, caduel!

where the binary part starts is difficult to find,

because the text part is a variable long text (a section of commentary),

then is die binary part,

no Mark!

I will try to find a good way to distinguish..

thanks again!:o