File and Datastream Functions¶
The
QIODeviceclass is the base interface class of all I/O devices in Qt Core .QIODeviceprovides both a common implementation and an abstract interface for devices that support reading and writing of blocks of data. The device can be a memory buffer, a file, or a datastream.Some subclasses like
QFilehave been implemented using a memory buffer for intermediate storing of data. This speeds up programs by reducing read/write operations. Buffering makes functions likegetChar()andputChar()fast, as they can operate on the memory buffer instead of directly on the device itself.The
QFileclass provides functions for reading from and writing to files. AQFilemay be used by itself or, more conveniently, with aQTextStreamorQDataStream.
QBufferallows you to access aQByteArrayusing theQIODeviceinterface. TheQByteArrayis treated just as a standard random-accessed file. An example:QBuffer buffer; char ch; buffer.open(QBuffer::ReadWrite); buffer.write("Qt rocks!"); buffer.seek(0); buffer.getChar(&ch); // ch == 'Q' buffer.getChar(&ch); // ch == 't' buffer.getChar(&ch); // ch == ' ' buffer.getChar(&ch); // ch == 'r'Call
open()to open the buffer. Then callwrite()orputChar()to write to the buffer, andread(),readLine(),readAll(), orgetChar()to read from it.size()returns the current size of the buffer, and you can seek to arbitrary positions in the buffer by callingseek(). When you are done with accessing the buffer, callclose().The
QDataStreamclass provides serialization of binary data to aQIODevice. A data stream is a binary stream of encoded information which is 100% inde- pendent of the host computer’s operating system, CPU or byte order. For example, a data stream that is written by a PC under Windows can be read by a Sun SPARC running Solaris. You can also use a data stream to read/write raw unencoded binary data.For more details on the datatypes that
QDataStreamcan serialize, see Serializing Qt Data Types .The
QTextStreamclass provides a convenient interface for reading and writing text.QTextStreamcan operate on aQIODevice, aQByteArrayor aQString. UsingQTextStream‘s streaming operators, you can conveniently read and write words, lines and numbers. It’s also common to useQTextStreamto read console input and write console output.There are three general ways to use
QTextStreamwhen reading text files:
Chunk by chunk, by calling
readLine()orreadAll().Word by word.
QTextStreamsupports streaming intoQStrings,QByteArrays and char* buffers. Words are delimited by space, and leading white space is automatically skipped.Character by character, by streaming into
QCharor char types. This method is often used for convenient input handling when parsing files, independent of character encoding and end-of-line semantics. To skip white space, callskipWhiteSpace().
QByteArraycan be used to store both raw bytes (including\0) and traditional 8-bit ‘\0’-terminated strings. UsingQByteArrayis much more convenient than using const char *. It always ensures that the data is followed by a ‘\0’ terminator, and uses implicitly shared classes (copy-on-write) to reduce memory usage and avoid needless copying of data.In addition to
QByteArray, Qt also provides theQStringclass to store string data. For most purposes,QStringis the most appropriate class to use. It stores 16-bit Unicode characters. It is, however, a good idea to useQByteArraywhen you need to store raw binary data, and when memory conservation is critical (for example, with Qt for Embedded Linux).
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.