summaryrefslogtreecommitdiffstats
path: root/src/corelib/doc/src/filestorage.qdoc
blob: e291ba1375a46c5d57173861f834c3c3f32b4d6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the documentation of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:FDL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Free Documentation License Usage
** Alternatively, this file may be used under the terms of the GNU Free
** Documentation License version 1.3 as published by the Free Software
** Foundation and appearing in the file included in the packaging of
** this file. Please review the following information to ensure
** the GNU Free Documentation License version 1.3 requirements
** will be met: https://www.gnu.org/licenses/fdl-1.3.html.
** $QT_END_LICENSE$
**
****************************************************************************/

/*!
\page io-functions.html
\title File and Datastream Functions

The QIODevice class is the base interface class of all I/O devices in
\l{Qt Core}.  QIODevice provides 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 QFile have been implemented using a memory buffer for
intermediate storing of data. This speeds up programs by reducing
read/write operations. Buffering makes functions like \l{QFile::}{getChar()} and
\l{QFile::}{putChar()} fast, as they can operate on the memory buffer instead of
directly on the device itself.

The QFile class provides functions for reading from and writing to files.
A QFile may be used by itself or, more conveniently, with a QTextStream or
QDataStream.

QBuffer allows you to access a QByteArray using the QIODevice interface.
The QByteArray is treated just as a standard random-accessed file.
An example:

\code
     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'
\endcode

Call \l{QBuffer::}{open()} to open the buffer. Then call \l{QBuffer::}{write()} or \l{QBuffer::}{putChar()} to write to
the buffer, and \l{QBuffer::}{read()}, \l{QBuffer::}{readLine()}, \l{QBuffer::}{readAll()}, or \l{QBuffer::}{getChar()} to read from it.
\l{QBuffer::}{size()} returns the current size of the buffer, and you can seek to arbitrary
positions in the buffer by calling \l{QBuffer::}{seek()}. When you are done with accessing
the buffer, call \l{QBuffer::}{close()}.

The QDataStream class provides serialization of binary data to a QIODevice.
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 QDataStream can serialize, see
\l{Serializing Qt Data Types}.

The QTextStream class provides a convenient interface for reading and
writing text. QTextStream can operate on a QIODevice, a QByteArray or
a QString. Using QTextStream's streaming operators, you can conveniently read
and write words, lines and numbers. It's also common to use QTextStream to
read console input and write console output.

There are three general ways to use QTextStream when reading text files:

\list
    \li Chunk by chunk, by calling \l{QBuffer::readLine()}{readLine()} or \l{QBuffer::readAll()}{readAll()}.
    \li Word by word. QTextStream supports streaming into \l{QString}s, \l{QByteArray}s
        and char* buffers. Words are delimited by space, and leading white space
        is automatically skipped.
    \li Character by character, by streaming into QChar or 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, call \l{QTextStream::}{skipWhiteSpace()}.
\endlist

QByteArray can be used to store both raw bytes (including \c{\0}) and traditional
8-bit '\\0'-terminated strings. Using QByteArray is much more convenient
than using const char *. It always ensures that the data is followed by a '\\0'
terminator, and uses \l{Implicit Sharing}{implicitly shared classes} (copy-on-write)
to reduce memory usage and avoid needless copying of data.

In addition to QByteArray, Qt also provides the QString class to store string
data. For most purposes, QString is the most appropriate class to use. It stores
16-bit Unicode characters. It is, however, a good idea to use QByteArray when you
need to store raw binary data, and when memory conservation is critical (for
example, with Qt for Embedded Linux).

*/