PDA

View Full Version : Read and Write file to/from QFile



ruben.rodrigues
31st March 2011, 15:39
Hi all!

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


//----------Read File----------//

QFile file(ui.lineEdit->text()); // /home/user/fileIN.txt
if(!file.open(QIODevice::ReadOnly))
qDebug()<<"Error opening the file";

QByteArray qba;

QDataStream dataStream(&qba, QIODevice::WriteOnly);
dataStream << file.readAll();


//---------Write File----------//

QByteArray outFile;

QDataStream dataStream2(&qba, QIODevice::ReadOnly);
dataStream2 >> outFile;

QFile out(ui.lineEdit_2->text()); // /home/user/fileOut.txt
if(!file.open(QIODevice::WriteOnly))
qDebug()<<"Error opening the file";

out.write(outFile);

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

wysota
31st March 2011, 15:57
Why are you using QDataStream?