Results 1 to 11 of 11

Thread: How to write the BYTE* data to a QFile?

  1. #1
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default How to write the BYTE* data to a QFile?

    How to write the BYTE* data to a QFile? I have binary data in BYTE*, I want to write that data completely to QFile. How to perform this. Please help me

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to write the BYTE* data to a QFile?

    I assume your BYTE is char type, then convert char* to QByteArray, something like this

    Qt Code:
    1. char* data; //binary data
    2. int length; //length of binary data
    3. QFile file;
    4.  
    5. file.write(QByteArray(data, length));
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to write the BYTE* data to a QFile?

    I am using BYTE* configid; How to write this to QFile.

  4. #4
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to write the BYTE* data to a QFile?

    how do you fill the BYTE*?

  5. #5
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to write the BYTE* data to a QFile?

    memcpy(configid,buff,len);
    here buff is QByteArray.

  6. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to write the BYTE* data to a QFile?

    is there any problem using
    Qt Code:
    1. QFile::write(QByteArray(configid, len));
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to write the BYTE* data to a QFile?

    Yes, it just prints one ",". Thats all.

  8. #8
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to write the BYTE* data to a QFile?

    Then check what is going wrong,

    - check if len is proper, set a break point at write and check the value of len
    - check QFile is opened in binary write mode

  9. #9
    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: How to write the BYTE* data to a QFile?

    Quote Originally Posted by Gokulnathvc View Post
    memcpy(configid,buff,len);
    here buff is QByteArray.
    If you mean that buff is a pointer to a QByteArray then this will not do what you are intending. If buff is the value of QByteArray::constData() then you are copying the data from the QByteArray (assuming len is meaningful). In any case you have invented work for yourself. If you have the data already in a QByteArray then don't copy is somewhere else before trying to put it in a file.
    Qt Code:
    1. // QByteArray buff;
    2. QFile out("/tmp/dump.bin");
    3. if (file.open(QIODevice::WriteOnly)) {
    4. open.write(buff);
    5. file.close();
    6. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to write the BYTE* data to a QFile?

    Quote Originally Posted by Santosh Reddy View Post
    Then check what is going wrong,

    - check if len is proper, set a break point at write and check the value of len
    - check QFile is opened in binary write mode
    Hello!

    How is it possible to "open in binary write mode"? The QIODevice::OpenMode allows only QFile::Text to be set, and no other option is available specifically to binary.

  11. #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: How to write the BYTE* data to a QFile?

    If you don't request the end-of-line translations with QIODevice::Text then no translation is done: it is a binary file... just a stream of bytes.

Similar Threads

  1. Replies: 4
    Last Post: 9th May 2011, 10:52
  2. How to write in a QFile
    By gorka_sm in forum Newbie
    Replies: 2
    Last Post: 5th May 2011, 13:08
  3. Replies: 13
    Last Post: 10th March 2011, 08:35
  4. PlEASE HELP: Hot To Use Byte Array, data stream
    By aash_89 in forum Qt Programming
    Replies: 7
    Last Post: 16th July 2010, 09:33
  5. QFile write isn't stable
    By ruben.rodrigues in forum Newbie
    Replies: 2
    Last Post: 8th July 2010, 22:14

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.