Results 1 to 3 of 3

Thread: Writing data in char pointer using QFile

  1. #1
    Join Date
    Sep 2012
    Posts
    31
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Writing data in char pointer using QFile

    Hi , I am having problems in writing data in a file. I have a file that contains binary code . This will serve as a dummy file that QFile will read and display in the GUI. So far, reading the data and displaying it has no issues.

    WHen try to write it into the same file, the file becomes blank.( Basically the contents are erased even if I used the same char pointer. I used debug mode and checked the contents of the char pointer. The data was still there but nothing was written on the file.

    here is sample code of writing:

    void DisplaySize::write_bin_file(unsigned long datasize, unsigned char *dataPtr, QString directory)
    {

    QFile DummyFile(directory);

    if(!DummyFile.open(QIODevice::WriteOnly | QIODevice::Text))
    return;

    QByteArray testing= "asfdsfd";
    unsigned char *newPtr = NULL;
    newPtr = (unsigned char *)malloc(datasize);
    memcpy(newPtr ,dataPtr,datasize);



    DummyFile.write((const char*)newPtr );




    DummyFile.close();
    }



    the Qbytearray testing is just to test whether I can write on the text file. If I used that, and use testing.constData() to get the char* pointer, it writes on the file.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Writing data in char pointer using QFile

    Try :
    Qt Code:
    1. DummyFile.write((const char*)newPtr,datasize );
    To copy to clipboard, switch view to plain text mode 
    Read in Qt doc what is difference.

  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: Writing data in char pointer using QFile

    You certainly do not want to open the file with the QIODevice::Text flag: it will mangle carriage returns and line feeds in your binary data on Windows.

    Why bother with all the copying and leaking memory? You have a pointer to the data and a data length... QIODevice::write() has a variant to fit. The whole function comes down to:
    Qt Code:
    1. void write_bin_file(unsigned long datasize, unsigned char *dataPtr, QString directory)
    2. {
    3. QFile DummyFile(directory);
    4.  
    5. if(DummyFile.open(QIODevice::WriteOnly)) {
    6. qint64 bytesWritten = DummyFile.write(reinterpret_cast<const char*>(dataPtr), datasize);
    7. if (bytesWritten < datasize) {
    8. // error
    9. }
    10. DummyFile.close();
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QFile BUG ? Problem with opening and writing
    By mlask in forum Qt Programming
    Replies: 2
    Last Post: 25th May 2012, 15:03
  2. Replies: 2
    Last Post: 9th August 2011, 08:37
  3. Replies: 1
    Last Post: 4th December 2010, 17:20
  4. QFile resized files gets truncated after writing
    By MaximA in forum Qt Programming
    Replies: 1
    Last Post: 24th May 2008, 17:23
  5. problem in writing text with QFile
    By wei243 in forum Qt Programming
    Replies: 5
    Last Post: 6th March 2007, 14:26

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.