Results 1 to 3 of 3

Thread: Write NULL character to a file

  1. #1
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Write NULL character to a file

    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 :

    Qt Code:
    1. QFile file("out.txt");
    2. if (!file.open(QIODevice::WriteOnly))
    3. return;
    4.  
    5. QRgb* rgb = (QRgb*)image.scanLine(0);
    6.  
    7. for (int x = 0; x < image.width(); x++)
    8. {
    9. int AlphaValue = qAlpha(rgb[x]);
    10. int RedValue = qRed(rgb[x]);
    11. int GreenValue = qGreen(rgb[x]);
    12. int BlueValue = qBlue(rgb[x]);
    13.  
    14.  
    15. QString AlphaAscii ((QChar)AlphaValue);
    16.  
    17. file.write(qPrintable(AlphaAscii));
    18.  
    19. //file.write((const char *)AlphaAscii.toAscii());
    20.  
    21. }
    22.  
    23. file.close();
    To copy to clipboard, switch view to plain text mode 
    Last edited by madshiva; 17th October 2011 at 16:13.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Write NULL character to a file

    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?
    Qt Code:
    1. for(...) {
    2. file.write((const char*)(rgb+x), 4);
    3. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    madshiva (18th October 2011)

  4. #3
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Write NULL character to a file

    Ok. Thanks you very much for your help !

Similar Threads

  1. Null character transmission in QTcpSocket
    By Manish.S in forum Qt Programming
    Replies: 4
    Last Post: 18th June 2010, 07:31
  2. QFile - QDataStream read and write each character
    By nhs_0702 in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2010, 19:03
  3. Character by Character (Unicode?) File Reading
    By mclark in forum Qt Programming
    Replies: 4
    Last Post: 22nd April 2009, 15:28
  4. NUll character output for combobox index
    By mdskpr778 in forum Qt Programming
    Replies: 2
    Last Post: 8th October 2008, 18:22
  5. How to terminate NULL character ?
    By merry in forum Qt Programming
    Replies: 3
    Last Post: 8th July 2008, 20:01

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.