#include#include int main() { QString filename = "example.txt"; QFile file(filename); if (!file.open(QIODevice::ReadOnly)) { qWarning("Could not open file"); return 1; } while (!file.atEnd()) { QByteArray line = file.readLine(); qDebug() << line; } file.close(); return 0; }
#includeThis code opens a file called "example.txt" in write-only mode, writes the string "Hello, world!" to it using QTextStream, and then closes the file. Package/library: Qt C++ library.int main() { QString filename = "example.txt"; QFile file(filename); if (!file.open(QIODevice::WriteOnly)) { qWarning("Could not open file"); return 1; } QTextStream out(&file); out << "Hello, world!\n"; file.close(); return 0; }