Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
filestorage.qdoc
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5\page io-functions.html
6\title File and Datastream Functions
7
8The QIODevice class is the base interface class of all I/O devices in
9\l{Qt Core}. QIODevice provides both a common implementation and an
10abstract interface for devices that support reading and writing of blocks
11of data. The device can be a memory buffer, a file, or a datastream.
12
13Some subclasses like QFile have been implemented using a memory buffer for
14intermediate storing of data. This speeds up programs by reducing
15read/write operations. Buffering makes functions like \l{QFile::}{getChar()} and
16\l{QFile::}{putChar()} fast, as they can operate on the memory buffer instead of
17directly on the device itself.
18
19The QFile class provides functions for reading from and writing to files.
20A QFile may be used by itself or, more conveniently, with a QTextStream or
21QDataStream.
22
23QBuffer allows you to access a QByteArray using the QIODevice interface.
24The QByteArray is treated just as a standard random-accessed file.
25An example:
26
27\code
28 QBuffer buffer;
29 char ch;
30
31 buffer.open(QBuffer::ReadWrite);
32 buffer.write("Qt rocks!");
33 buffer.seek(0);
34 buffer.getChar(&ch); // ch == 'Q'
35 buffer.getChar(&ch); // ch == 't'
36 buffer.getChar(&ch); // ch == ' '
37 buffer.getChar(&ch); // ch == 'r'
38\endcode
39
40Call \l{QBuffer::}{open()} to open the buffer. Then call \l{QBuffer::}{write()} or \l{QBuffer::}{putChar()} to write to
41the buffer, and \l{QBuffer::}{read()}, \l{QBuffer::}{readLine()}, \l{QBuffer::}{readAll()}, or \l{QBuffer::}{getChar()} to read from it.
42\l{QBuffer::}{size()} returns the current size of the buffer, and you can seek to arbitrary
43positions in the buffer by calling \l{QBuffer::}{seek()}. When you are done with accessing
44the buffer, call \l{QBuffer::}{close()}.
45
46The QDataStream class provides serialization of binary data to a QIODevice.
47A data stream is a binary stream of encoded information which is 100% inde-
48pendent of the host computer's operating system, CPU or byte order. For
49example, a data stream that is written by a PC under Windows can be read
50by a Sun SPARC running Solaris. You can also use a data stream to read/write
51raw unencoded binary data.
52
53For more details on the datatypes that QDataStream can serialize, see
54\l{Serializing Qt Data Types}.
55
56The QTextStream class provides a convenient interface for reading and
57writing text. QTextStream can operate on a QIODevice, a QByteArray or
58a QString. Using QTextStream's streaming operators, you can conveniently read
59and write words, lines and numbers. It's also common to use QTextStream to
60read console input and write console output.
61
62There are three general ways to use QTextStream when reading text files:
63
64\list
65 \li Chunk by chunk, by calling \l{QBuffer::readLine()}{readLine()} or \l{QBuffer::readAll()}{readAll()}.
66 \li Word by word. QTextStream supports streaming into \l{QString}s, \l{QByteArray}s
67 and char* buffers. Words are delimited by space, and leading white space
68 is automatically skipped.
69 \li Character by character, by streaming into QChar or char types. This
70 method is often used for convenient input handling when parsing files,
71 independent of character encoding and end-of-line semantics. To skip
72 white space, call \l{QTextStream::}{skipWhiteSpace()}.
73\endlist
74
75QByteArray can be used to store both raw bytes (including \c{\0}) and traditional
768-bit '\\0'-terminated strings. Using QByteArray is much more convenient
77than using const char *. It always ensures that the data is followed by a '\\0'
78terminator, and uses \l{Implicit Sharing}{implicitly shared classes} (copy-on-write)
79to reduce memory usage and avoid needless copying of data.
80
81In addition to QByteArray, Qt also provides the QString class to store string
82data. For most purposes, QString is the most appropriate class to use. It stores
8316-bit Unicode characters. It is, however, a good idea to use QByteArray when you
84need to store raw binary data, and when memory conservation is critical (for
85example, with Qt for Embedded Linux).
86
87*/