QFile file("myfile.txt"); if(!file.open(QIODevice::ReadOnly)){ qDebug() << "Error opening file: " << file.errorString(); }
QFile file("myfile.txt"); if(!file.open(QIODevice::WriteOnly)){ qDebug() << "Error opening file: " << file.errorString(); } if(file.write("Hello World") == -1){ qDebug() << "Error writing to file: " << file.errorString(); }This code opens a file named myfile.txt in write-only mode. If the file cannot be opened for any reason, the errorString() method of QFile is called to get the error message. If the write() method returns -1, it means that an error occurred while writing to the file. Package library: QFile is part of the Qt Core library, which is a collection of C++ classes for cross-platform application development. The Qt Core library is included in the Qt framework and is required to be installed to use QFile. Therefore, the package library for QFile is Qt.