Writing to a local file in Qt6 Windows

  • 1 year ago
Comment

1 / 1

To write to a local file in Qt6 on Windows, you can use the QFile class to create and write to a file. Here's an example:

cpp
#include <QFile>
#include <QTextStream>

void writeToFile()
{
QString filePath = "C:/path/to/file.txt"; // change this to your desired file path
QFile file(filePath);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return;

QTextStream out(&file);
out << "Hello, world!\n"; // write some text to the file

file.close();
}

In this example, QFile is used to create a file at the specified file path, with the WriteOnly and Text flags. QTextStream is used to write to the file, and then the file is closed with file.close().

Note that you may need to adjust the file path to match your specific use case. Also, make sure that you have permission to write to the specified file location.