#includeint main() { QFile file("example.txt"); if (file.open(QIODevice::WriteOnly)) { char data[] = "This is a test"; qint64 bytesWritten = file.writeBlock(data, sizeof(data)); //writes data to file file.close(); } return 0; }
#includeIn this example, we use the same code as Example 1 but change the number of bytes to be written to only the first 5 characters of the "data" array. This means that only the text "This " will be written to the file. The QFile class is part of the Qt Core library, which is included in the Qt framework.int main() { QFile file("example.txt"); if (file.open(QIODevice::WriteOnly)) { char data[] = "This is a test"; qint64 bytesWritten = file.writeBlock(data, 5); //writes only the first 5 characters of data to file file.close(); } return 0; }