Results 1 to 11 of 11

Thread: unsigned char write to file

  1. #1
    Join Date
    Dec 2013
    Posts
    20
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default unsigned char write to file

    Hello,how can we write unsigned char to file.
    i used write() but i cant

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: unsigned char write to file

    Since that is done using write() could you show the code that you are using and how you determine that it didn' work?

    Cheers,
    __

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: unsigned char write to file

    Not clear if you mean a single unsigned char or a bunch of them:
    Qt Code:
    1. unsigned char a = 0x00;
    2. unsigned char data[] = { 0x00, 0x01, 0x02, 0xff };
    3. QFile file("test.bin");
    4. if (file.open(QIODevice::WriteOnly)) {
    5. file.write(reinterpret_cast<char *>(&a), sizeof(a));
    6. file.write(reinterpret_cast<char *>(&data), sizeof(data));
    7. file.close();
    8. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. $ od -tx1 test.bin
    2. 0000000 00 00 01 02 ff
    To copy to clipboard, switch view to plain text mode 

  4. The following user says thank you to ChrisW67 for this useful post:

    seniorc (20th December 2013)

  5. #4
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: unsigned char write to file

    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.

  6. #5
    Join Date
    Oct 2013
    Location
    Bangalore,India
    Posts
    64
    Thanks
    21
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: unsigned char write to file

    You can write unsigned char *.Here is one of the my sample code
    Can get idea from this:

    Qt Code:
    1. unsigned char* data3d = new unsigned char[x_size*y_size];
    2. int i,j,k=0;
    3. fp = fopen (output_str, "wb");
    4. for ( i = x_size; i >= 0; i--)
    5. for (j = 0; j < y_size; j++)
    6. {
    7. data3d[k++]=Buffer_in[i][j];
    8. }
    9. fwrite( data3d, sizeof( char ),x_size*y_size, fp);
    10. fclose(fp);
    To copy to clipboard, switch view to plain text mode 

  7. #6
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: unsigned char write to file

    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).

  8. #7
    Join Date
    Dec 2013
    Posts
    20
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: unsigned char write to file

    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?
    Qt Code:
    1. QString compressed // inside bit example: 1010101010111...
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. for (;!(compressed.isEmpty());compressed=compressed.mid(8)){
    2. unsigned char byte = 0;
    3. for (int j=0; j<8; ++j) {
    4. if (compressed.at(j) == '1')
    5. byte |= 0x01;
    6. else
    7. byte &= ~(0x01);
    8. if (j < 7) byte<<=1;
    9. }
    10. codefile.write(reinterpret_cast<char *>(&byte),sizeof(byte));
    11. }
    To copy to clipboard, switch view to plain text mode 

    Thanks
    Last edited by seniorc; 21st December 2013 at 11:32.

  9. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: unsigned char write to file

    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,
    _

  10. #9
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: unsigned char write to file

    Make in advance everything you can do in advance and go "low level" avoiding as much of unnecessary actions you can do. For example:
    Qt Code:
    1. const QChar *dd = compressed.constData();
    2. const int N = compressed.size();
    3. int k = 0;
    4. unsigned char buff = 0;
    5. unsigned char bits[8] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
    6.  
    7. const QChar Zero('0');
    8. const QChar One('1');
    9.  
    10. for( int i = 0; i < N; i++ )
    11. {
    12. if( dd[i] == Zero ) k++;
    13. else if( dd[i] = One )
    14. {
    15. buff |= bits[k];
    16. k++;
    17. }
    18. else
    19. {
    20. report error;
    21. break;
    22. }
    23.  
    24. if( k == 8 )
    25. {
    26. ff.write(reinterpret_cast<char *>(&buff),sizeof(unsigned char));
    27. buff = 0;
    28. k = 0;
    29. }
    30. }
    31.  
    32. if( k > 0 ) // incomplete byte
    33. {
    34. ff.write(reinterpret_cast<char *>(&buff),sizeof(unsigned char));
    35. set some flag, only k bits valid;
    36. }
    To copy to clipboard, switch view to plain text mode 

  11. The following user says thank you to Radek for this useful post:

    seniorc (21st December 2013)

  12. #10
    Join Date
    Dec 2013
    Posts
    20
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: unsigned char write to file

    Quote Originally Posted by Radek View Post
    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 http://doc.qt.io/qt-4.8/QDataStream but is there a better way?for example;write QMap and read QMap for easy way?

  13. #11
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: unsigned char write to file

    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.

Similar Threads

  1. How to convert unsigned char[] to char *?
    By Gokulnathvc in forum Newbie
    Replies: 2
    Last Post: 29th April 2011, 08:58
  2. how can we assign value in unsigned char[]
    By gauravg in forum General Programming
    Replies: 1
    Last Post: 13th April 2011, 08:44
  3. unsigned char * to QString
    By elina.du in forum Qt Programming
    Replies: 2
    Last Post: 25th March 2009, 08:33
  4. Conversion from unsigned char* to unsigned char
    By santosh.kumar in forum General Programming
    Replies: 1
    Last Post: 6th August 2007, 13:12
  5. QString to unsigned char *
    By darksaga in forum Qt Programming
    Replies: 9
    Last Post: 23rd July 2007, 07:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.