QFile file("data.txt"); if(file.isOpen()) { // do some operations on the file } else { // file is not open }
QFile file; file.setFileName("data.txt"); file.open(QIODevice::ReadOnly | QIODevice::Text); if(file.isOpen()) { // read data from the file } else { // error opening the file }In this example, we create a QFile object and set the file name using setFileName() method. We then open the file for reading using the open() method. After that, we use the isOpen() method to check if the file is open or not. If the file is open, we proceed to read data from it, otherwise we handle the error. The package library for QFile is Qt.