Reading and writing files

Once you know the path to a file (for example, using QDir::entryList(), QFileDialog::getOpenFileName(), or some external source), you can pass it to QFile to receive an object that acts as a handle to the file. Before the file contents can be accessed, the file needs to be opened using the open() method. The basic variant of this method takes a mode in which we need to open the file. The following table explains the modes that are available:

Mode Description

ReadOnly

This file can be read from.

WriteOnly

This file can be written to.

ReadWrite

This file can be read from and written to.

Append

All data writes will be written at the end of the file.

Truncate

If the file is present, its ...

Get Game Programming using Qt 5 Beginner's Guide - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.