Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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
8
The 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
10
abstract interface for devices that support reading and writing of blocks
11
of data. The device can be a memory buffer, a file, or a datastream.
12
13
Some subclasses like QFile have been implemented using a memory buffer for
14
intermediate storing of data. This speeds up programs by reducing
15
read/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
17
directly on the device itself.
18
19
The QFile class provides functions for reading from and writing to files.
20
A QFile may be used by itself or, more conveniently, with a QTextStream or
21
QDataStream.
22
23
QBuffer allows you to access a QByteArray using the QIODevice interface.
24
The QByteArray is treated just as a standard random-accessed file.
25
An 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
40
Call \l{QBuffer::}{open()} to open the buffer. Then call \l{QBuffer::}{write()} or \l{QBuffer::}{putChar()} to write to
41
the 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
43
positions in the buffer by calling \l{QBuffer::}{seek()}. When you are done with accessing
44
the buffer, call \l{QBuffer::}{close()}.
45
46
The QDataStream class provides serialization of binary data to a QIODevice.
47
A data stream is a binary stream of encoded information which is 100% inde-
48
pendent of the host computer's operating system, CPU or byte order. For
49
example, a data stream that is written by a PC under Windows can be read
50
by a Sun SPARC running Solaris. You can also use a data stream to read/write
51
raw unencoded binary data.
52
53
For more details on the datatypes that QDataStream can serialize, see
54
\l{Serializing Qt Data Types}.
55
56
The QTextStream class provides a convenient interface for reading and
57
writing text. QTextStream can operate on a QIODevice, a QByteArray or
58
a QString. Using QTextStream's streaming operators, you can conveniently read
59
and write words, lines and numbers. It's also common to use QTextStream to
60
read console input and write console output.
61
62
There 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
75
QByteArray can be used to store both raw bytes (including \c{\0}) and traditional
76
8-bit '\\0'-terminated strings. Using QByteArray is much more convenient
77
than using const char *. It always ensures that the data is followed by a '\\0'
78
terminator, and uses \l{Implicit Sharing}{implicitly shared classes} (copy-on-write)
79
to reduce memory usage and avoid needless copying of data.
80
81
In addition to QByteArray, Qt also provides the QString class to store string
82
data. For most purposes, QString is the most appropriate class to use. It stores
83
16-bit Unicode characters. It is, however, a good idea to use QByteArray when you
84
need to store raw binary data, and when memory conservation is critical (for
85
example, with Qt for Embedded Linux).
86
87
*/
qtbase
src
corelib
doc
src
filestorage.qdoc
Generated on
for Qt by
1.14.0