PDA

View Full Version : unsigned char write to file



seniorc
20th December 2013, 09:50
Hello,how can we write unsigned char to file.
i used write() but i cant

anda_skoa
20th December 2013, 11:06
Since that is done using write() could you show the code that you are using and how you determine that it didn' work?

Cheers,
__

ChrisW67
20th December 2013, 11:21
Not clear if you mean a single unsigned char or a bunch of them:


unsigned char a = 0x00;
unsigned char data[] = { 0x00, 0x01, 0x02, 0xff };
QFile file("test.bin");
if (file.open(QIODevice::WriteOnly)) {
file.write(reinterpret_cast<char *>(&a), sizeof(a));
file.write(reinterpret_cast<char *>(&data), sizeof(data));
file.close();
}



$ od -tx1 test.bin
0000000 00 00 01 02 ff

Radek
20th December 2013, 17:41
A possible cause of the problem: You can write either char * or QByteArray. Because of type checking, you cannot write an unsigned char *. Do as ChrisW67 has shown and reinterpret_cast.

prkhr4u
21st December 2013, 05:01
You can write unsigned char *.Here is one of the my sample code
Can get idea from this:


unsigned char* data3d = new unsigned char[x_size*y_size];
int i,j,k=0;
fp = fopen (output_str, "wb");
for ( i = x_size; i >= 0; i--)
for (j = 0; j < y_size; j++)
{
data3d[k++]=Buffer_in[i][j];
}
fwrite( data3d, sizeof( char ),x_size*y_size, fp);
fclose(fp);

Radek
21st December 2013, 07:28
This is ANSI C, not Qt, prkhr4u. The pointer in fwrite() is void * so that you can write anything you want. The pointer in QIODevice::write() is char * (or a reference to a QByteArray).

seniorc
21st December 2013, 12:24
I done writing unsigned char to file,ChrisW67's way.But i have new problem.Here is my codes.very slow run in this line.How can i do faster than now.Any idea?


QString compressed // inside bit example: 1010101010111...


for (;!(compressed.isEmpty());compressed=compressed.mi d(8)){
unsigned char byte = 0;
for (int j=0; j<8; ++j) {
if (compressed.at(j) == '1')
byte |= 0x01;
else
byte &= ~(0x01);
if (j < 7) byte<<=1;
}
codefile.write(reinterpret_cast<char *>(&byte),sizeof(byte));
}

Thanks

anda_skoa
21st December 2013, 14:38
My guess would be that the mid() call makes it slow due to having to create a new string and copy the remaining data.

Better loop over the whole string and write a byte every 8th character.

Additionally you could also preallocate a QByteArray with compressed.length / 8 size and fill it as you go, then write that block to the file in one go.

Cheers,
_

Radek
21st December 2013, 17:32
Make in advance everything you can do in advance and go "low level" avoiding as much of unnecessary actions you can do. For example:


const QChar *dd = compressed.constData();
const int N = compressed.size();
int k = 0;
unsigned char buff = 0;
unsigned char bits[8] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };

const QChar Zero('0');
const QChar One('1');

for( int i = 0; i < N; i++ )
{
if( dd[i] == Zero ) k++;
else if( dd[i] = One )
{
buff |= bits[k];
k++;
}
else
{
report error;
break;
}

if( k == 8 )
{
ff.write(reinterpret_cast<char *>(&buff),sizeof(unsigned char));
buff = 0;
k = 0;
}
}

if( k > 0 ) // incomplete byte
{
ff.write(reinterpret_cast<char *>(&buff),sizeof(unsigned char));
set some flag, only k bits valid;
}

seniorc
21st December 2013, 22:29
Make in advance...

Thanks I gained twelve seconds,How can I improve better low level programming whats your suggestions,for example;thats my mistakes?I've got a new question.QMap,QHash,QList write file?I used QDataStream but is there a better way?for example;write QMap and read QMap for easy way?

ChrisW67
22nd December 2013, 02:51
You could save yourself a bunch of manipulations if your built your binary data up front rather than building a string full of '1' and '0' characters and then having to convert it.