QFileInfo file("example.txt"); if (file.isWritable()) { QFile data(file.filePath()); if (data.open(QFile::WriteOnly | QFile::Truncate)) { QTextStream out(&data); out << "Hello, world!"; data.close(); } }
QFileInfo file("new_file.txt"); if (file.isWritable()) { QFile data(file.filePath()); if (data.open(QFile::WriteOnly | QFile::Truncate)) { data.close(); } }In this example, we create a QFileInfo object representing a new file (which doesn't yet exist). We check if the directory where we plan to create the file is writable using isWritable(). If it is, we attempt to create the file using QFile and close it. The QFileInfo class is part of the QtCore module, which is included in the Qt library for C++.