PDA

View Full Version : Reading all from a file that contains "\0" character



^NyAw^
24th November 2011, 17:17
Hi,

I'm reading a text file with QFile that may contain "\0" characters.
The problem is that QFile::read() returns only first characters to the first "\0" occurence.

Anyone know how to read all the characters from the file to the end?

Thanks,

stampede
24th November 2011, 18:59
Try with QDataStream :: readRawData:


QFile file("test.txt");
if( file.open(QIODevice::WriteOnly) ){
file.putChar('a');
file.putChar('\0');
file.putChar('b');
file.close();
if( file.open(QIODevice::ReadOnly) ){
const int size = file.size();
QByteArray ba(size,'x');
QDataStream stream(&file);
stream.readRawData(ba.data(),size);
qDebug() << ba.length();
if( ba.length() > 2 ){
qDebug() << (ba[0] == 'a') << (ba[1] == '\0') << (ba[2] == 'b');
}
}
}


Looks like it works ok for me, output is:


warning: 3
warning: true true true

^NyAw^
25th November 2011, 08:52
Hi,

I've used "QQFile::peek" that does the job.

Now the problem is that the QByteArray that contains the data shows that only contains data until the "\0" character but it's size is larger. I need to use all the data readed and when unsing "QByteArray::mid" it only gets the text untill "\0".

Thanks,

ChrisW67
26th November 2011, 06:01
Hi,

I'm reading a text file with QFile that may contain "\0" characters.
The problem is that QFile::read() returns only first characters to the first "\0" occurence.

No it doesn't. QIODevice::read() reads up to the specified number of bytes from the stream, in this case a file. It doesn't care what the bytes are.


#include <QtCore>
#include <QDebug>

int main(int argc, char **argv) {
QCoreApplication app(argc, argv);

// Write a test file
QByteArray data("ABCD\0FGHI\0JK", 12);
QFile test("test.bin");
if (test.open(QIODevice::WriteOnly)) {
qint64 wrote = test.write(data);
qDebug() << "Wrote" << wrote << "of" << data.size() << "bytes";
test.close();
}

// Now read it back
if (test.open(QIODevice::ReadOnly)) {
QByteArray result = test.readAll();
qDebug() << "readAll() == " << result.size() << "bytes";

test.seek(0);
result = test.read(1000);
qDebug() << "read() == " << result.size() << "bytes";

test.close();
}

return app.exec();
}

Output:


Wrote 12 of 12 bytes
readAll() == 12 bytes
read() == 12 bytes

myta212
27th November 2011, 15:17
Hi,
I think the simple method to solve your problem if your input file is too big is use this step:
1. get size of your input file (use fseek)
2. set size buffer to read file (nbuffer). You can set nbuffer=1 if you want to read your input file every 1 byte, nbuffer=200 read every 200 byte, etc.
3. read file with size nbuffer.
4. check buffer. loop from 0 to nbuffer-1. If you dont get "\0" charater, go to number 3. If you get "\0" chacracter, exit loop and get "\0" location.

I use this method when reading a big file binary data (more than 6Gb) and success.
I think this is simple. Are you think this is solve your problem?

Best regards,

Myta

^NyAw^
28th November 2011, 09:24
Hi,

Thanks all, I've solved my problem.

Spitfire
28th November 2011, 10:00
If you can, post your solution so other can solve their problems when they get across this thread.
Cheers!

^NyAw^
28th November 2011, 15:54
Hi,

As ChrisW67 reported, it reads all the bytes. The problem was on using this into a QByteArray or a QString object as this classes use "\0" as end of string.
So here I had the problem. As I'm who is writing the file content, I just changed the way that the data is stored. I used QByteArray::toBase64 before writing to the file.

So, Spitfire there is no really a solution to post.