PDA

View Full Version : Write NULL character to a file



madshiva
17th October 2011, 15:55
Hello,

I have try to find the answer, but didn't have found one. I have a small problem to write NULL character to a file, the problem is that the character is not write.

Could somebody help me ?

I have tried with QDataStream but It's add many NULL char that I don't want.

Thanks.


This is the code that I use :


QFile file("out.txt");
if (!file.open(QIODevice::WriteOnly))
return;

QRgb* rgb = (QRgb*)image.scanLine(0);

for (int x = 0; x < image.width(); x++)
{
int AlphaValue = qAlpha(rgb[x]);
int RedValue = qRed(rgb[x]);
int GreenValue = qGreen(rgb[x]);
int BlueValue = qBlue(rgb[x]);


QString AlphaAscii ((QChar)AlphaValue);

file.write(qPrintable(AlphaAscii));

//file.write((const char *)AlphaAscii.toAscii());

}

file.close();

wysota
17th October 2011, 16:30
You can't write a null character using qPrintable because it terminates when \0 is encountered. Besides your code doesn't make any sense, you can cast between int and QChar.

Why don't you write the values directly?

for(...) {
file.write((const char*)(rgb+x), 4);
}

madshiva
18th October 2011, 09:37
Ok. Thanks you very much for your help !