#include#include int main() { QString fileName = "/path/to/file.txt"; QFile file(fileName); if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) { qDebug() << "Failed to open file: " << fileName; return -1; } QTextStream in(&file); while(!in.atEnd()) { QString line = in.readLine(); qDebug() << line; } file.close(); return 0; }
#includeIn this example, we create a QFile object with the path to the new file we want to create. We open the file in write mode and use a QTextStream object to write to the file. We write the string "Hello, world!" to the file and close the file. This is a simple way to create a new file and write some text to it. Package library: Qt Core library#include int main() { QString fileName = "/path/to/newfile.txt"; QFile file(fileName); if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) { qDebug() << "Failed to open file: " << fileName; return -1; } QTextStream out(&file); out << "Hello, world!" << endl; file.close(); return 0; }