Results 1 to 2 of 2

Thread: Read and Write file to/from QFile

  1. #1
    Join Date
    Jun 2010
    Posts
    100
    Thanks
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Read and Write file to/from QFile

    Hi all!

    I need help in a small function to get a file back from a QFile that is in a QByteArray.

    Qt Code:
    1. //----------Read File----------//
    2.  
    3. QFile file(ui.lineEdit->text()); // /home/user/fileIN.txt
    4. if(!file.open(QIODevice::ReadOnly))
    5. qDebug()<<"Error opening the file";
    6.  
    7.  
    8. QDataStream dataStream(&qba, QIODevice::WriteOnly);
    9. dataStream << file.readAll();
    10.  
    11.  
    12. //---------Write File----------//
    13.  
    14. QByteArray outFile;
    15.  
    16. QDataStream dataStream2(&qba, QIODevice::ReadOnly);
    17. dataStream2 >> outFile;
    18.  
    19. QFile out(ui.lineEdit_2->text()); // /home/user/fileOut.txt
    20. if(!file.open(QIODevice::WriteOnly))
    21. qDebug()<<"Error opening the file";
    22.  
    23. out.write(outFile);
    To copy to clipboard, switch view to plain text mode 

    I have no clue how to get the from a qbytearray and write it to my disk. At the moment it does nothing...
    I need this because a co-worker of mine that doesn't work with me anymore wrote a function that save files to a database in form a a BLOB and now I don't know how to get them back.

    Thank you!


    Added after 5 minutes:


    Problem solved...I am so silly that I open the QFile file twice instead of the out...

    thanks anyway
    Last edited by ruben.rodrigues; 31st March 2011 at 14:39.

  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: Read and Write file to/from QFile

    Why are you using QDataStream?
    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.


Similar Threads

  1. Replies: 2
    Last Post: 2nd November 2010, 05:15
  2. QFile write and file modyfication time
    By Talei in forum Newbie
    Replies: 1
    Last Post: 9th May 2010, 20:46
  3. QFile - QDataStream read and write each character
    By nhs_0702 in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2010, 19:03
  4. Unable to write to file QFile
    By cuter in forum Qt Programming
    Replies: 4
    Last Post: 15th July 2009, 11:19
  5. How to write bytes read from serial port to a QFile
    By shamik in forum Qt Programming
    Replies: 19
    Last Post: 25th June 2007, 14:12

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.