// Creating a new file and writing to it #include#include int main() { QFile file("/path/to/file.txt"); if (file.open(QIODevice::WriteOnly | QIODevice::Text)) { QTextStream stream(&file); stream << "Hello, world!\n"; file.close(); } return 0; }
// Reading from an existing file #includeThis code reads the first line from an existing file and outputs it to the console using QFile. The QFile class is part of the Qt Core module, which is included in the Qt library. To use it in your C++ project, you need to include the appropriate header files and link against the Qt library.#include int main() { QFile file("/path/to/file.txt"); if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { QTextStream stream(&file); QString line = stream.readLine(); file.close(); qDebug() << line; } return 0; }