I want to use temporary files in Qt project
I try this code:
QTemporaryFile file;
file.open();
QTextStream stream(&file);
stream << content; // content is a QString
qDebug() << file.readAll();
But console shows an empty string:
""
How can I write a string in a QTemporaryFile?
Everything is working fine. QTemporaryFile always opens as ReadWrite, and is a random-access device, which means that after you write data, you either need to close and re-open it (which is an overkill), or go to the beginning of the file to read it:
QTemporaryFile file;
file.open();
QTextStream stream(&file);
stream << content; // here you write data into file.
//Your current position in the file is at it's end,
//so there is nothing for you to read.
stream.flush();//flush the stream into the file
file.seek(0); //go to the begining
qDebug() << file.readAll(); //read stuff
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With