How can I open a QFile for appending, i.e. the equivalent of
FILE *f = fopen("myfile.txt", "a");
To write text, we can use operator<<(), which is overloaded to take a QTextStream on the left and various data types (including QString) on the right: QFile file("out. txt"); if (! file.
QFile is an I/O device for reading and writing text and binary files and resources. A QFile may be used by itself or, more conveniently, with a QTextStream or QDataStream. The file name is usually passed in the constructor, but it can be set at any time using setFileName().
This way, the file must have a filename associated with it. So the first thing you have to do is create a Qt widget application. You will have to place 2 elements within this widget application: a text edit and a push button. We label the push button, 'Save As', as it functions as a 'Save As' button.
Open the file in QIODevice::Append
mode:
QFile f(...);
if (f.open(QIODevice::WriteOnly | QIODevice::Append)) {
...
}
See also the documentation.
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