#includeint main() { QFile file("example.txt"); if(file.open(QIODevice::ReadWrite)) { // File is opened file.close(); } return 0; }
#includeBrief Description of Examples: Example 1 opens a file "example.txt" in read-write mode using QFile class and then closes it using QFile close function. The code first creates a QFile object with the file name, then uses the open function with QIODevice::ReadWrite enum parameter to open the file for reading and writing. After the file is opened, close function is called to close the file. Example 2 also opens a file "example.txt" in read-write mode using QFile class and writes "Sample text" to the file using QTextStream. After writing to the file, close function is called. Package/Library: QFile belongs to Qt Core library. It is included in the QtCore package in Qt framework.#include int main() { QFile file("example.txt"); if(file.open(QIODevice::ReadWrite)) { // File is opened QTextStream stream(&file); stream << "Sample text" << endl; file.close(); } return 0; }