[QT] qdatastream class reference

Source: Internet
Author: User
Tags printable characters

The qdatastream class serializes binary data to qiodevice.

# Include <qdatastream. h>

Institute
List of member functions.

Public Member
  • Qdatastream
    ()
  • Qdatastream
    (Qiodevice * D)
  • Qdatastream
    (Qbytearray A, int Mode)
  • Virtual~ Qdatastream
    ()
  • Qiodevice *Device
    () Const
  • VoidSetdevice
    (Qiodevice * D)
  • VoidUnsetdevice
    ()
  • BoolAtend
    () Const
  • Bool EOF () const(Obsolete)
  • EnumByteorder
    {Bigendian, littleendian}
  • IntByteorder
    () Const
  • VoidSetbyteorder
    (INT Bo)
  • BoolIsprintabledata
    () Const
  • VoidSetprintabledata
    (Bool enable)
  • IntVersion
    () Const
  • VoidSetversion
    (Int v)
  • Qdatastream &Operator>
    (Q_int8 & I)
  • Qdatastream &Operator>
    (Q_uint8 & I)
  • Qdatastream &Operator>
    (Q_int16 & I)
  • Qdatastream &Operator>
    (Q_uint16 & I)
  • Qdatastream &Operator>
    (Q_int32 & I)
  • Qdatastream &Operator>
    (Q_uint32 & I)
  • Qdatastream &Operator>
    (Q_long & I)
  • Qdatastream &Operator>
    (Q_ulong & I)
  • Qdatastream &Operator>
    (Float & F)
  • Qdatastream &Operator>
    (Double & F)
  • Qdatastream &Operator>
    (Char * & S)
  • Qdatastream &Operator <
    (Q_int8 I)
  • Qdatastream &Operator <
    (Q_uint8 I)
  • Qdatastream &Operator <
    (Q_int16 I)
  • Qdatastream &Operator <
    (Q_uint16 I)
  • Qdatastream &Operator <
    (Q_int32 I)
  • Qdatastream &Operator <
    (Q_uint32 I)
  • Qdatastream &Operator <
    (Q_long I)
  • Qdatastream &Operator <
    (Q_ulong I)
  • Qdatastream &Operator <
    (Float F)
  • Qdatastream &Operator <
    (Double F)
  • Qdatastream &Operator <
    (Const char * s)
  • Qdatastream &Readbytes
    (Char * & S, uint & L)
  • Qdatastream &Readrawbytes
    (Char * s, uint Len)
  • Qdatastream &Writebytes
    (Const char * s, uint Len)
  • Qdatastream &Writerawbytes
    (Const char * s, uint Len)

Detailed description

The qdatastream class serializes binary data to qiodevice.

 

A Data Stream is a binary stream of encoding information. It has nothing to do with the host's operating system, CPU, or byte sequence of 100%. For example, a data stream written in Windows on a PC can be stored in Sun
It is read from the Solaris.

Qtextstream.
You can also use a data stream to read/write original unencoded binary data.
If you want to "parse" the input stream, see qtextstream.

The qdatastream class serializes basic types, suchChar
,Short
,Int
,Char *
And so on
. Serialization of more complex types is achieved by dividing data into simple units.

Data Streams work closely with qiodevice. Qiodevice describes an input/output medium that can read data from and write data to it. The qfile class is an example of an IO Device.

Instance (write binary data to a stream ):

Qfile F ("file. DTA ");
F. Open (io_writeonly );
Qdatastream S (& F); // We Will serialize data to file F
S <"the answer is"; // serializes a string
S <(q_int32) 42; // serializes an integer.

Instance (read binary data from a stream ):

Qfile F ("file. DTA ");
F. Open (io_readonly );
Qdatastream S (& F); // read serialized data from file F
Qstring STR;
Q_int32;
S> STR> A; // extract "the answer is" and 42

Every item to be written to the stream is written into a predefined binary format, which depends on the type of the item. The supported types in QT include qbrush, qcolor, qdatetime, qfont, qpixmap, qstring, qvariant, and others. Supported quantity
For a list of all the QT types of data streams, see qdatastream operations.
Format.

For example,Char *
The string is written as a 32-bit integer that is equal to the length of the string containing NUL bytes, followed by the string containing NUL bytes
All bytes. When readingChar *
When reading a string, first read 4 bytes to create a 32-bit length value, and then read so many bytes including NULChar *
Word
Character string.

The initial iodevice is usually set in the constructor, but you can also use setdevice ()
To change. If you reach the data endpoint (or if no iodevice is set), atend () will
Returns true.

If you want the data to be consistent with the QT of the previous version, use setversion ().

If you want the data to be readable, for example, for debugging, you can use setprintabledata ()
Set the data stream to the printable data mode. Then the data is written slowly and expanded, but it is already a readable format.

If you are generating a new binary data format, such as a file format created by your application, you can use qdatastream to write data into a portable format.
Generally, you can write a brief header containing the phantom string and version information, which will provide you with some space for future expansion. For example:

 

Qfile F ("file. XXX ");
F. Open (io_writeonly );
Qdatastream S (& F );

// Write a header containing "magic number" and version number
S <(q_uint32) 0xa0b0c0d0;
S <(q_int32) 123;

// Write data
S <[lots of interesting data]

Then read:

 

Qfile F ("file. XXX ");
F. Open (io_readonly );
Qdatastream S (& F );

// Read and check the header
Q_uint32 magic;
S> magic;
If (magic! = 0xa0b0c0d0)
Return xxx_bad_file_format;

// Read the version number
Q_int32 version;
S> version;
If (version <100)
Return xxx_bad_file_too_old;
If (version> 123)
Return xxx_bad_file_too_new;
If (version <= 110)
S. setversion (1 );

// Read data
S> [a lot of interesting data];
If (version> 120)
S> [new data XXX in version 1.2];
S> [other interesting data];

When serializing data, you can select the byte sequence you want to use. The default value is the first byte in height. Changing it to a low byte will undermine portability (unless the reader uses a low byte before it ).
We recommend that you use the default settings unless you have special needs.

Read/write original binary data

You may want to write your original binary data directly to the data stream or directly read them from the data stream. Data can be used
Readrawbytes ()
Read a pre-allocated char * from the stream *. You can also use writerawbytes ()
Write Data to the stream. Note that encoding/Decoding of any data can only be done by yourself.

A pair of similar functions readbytes ()
And writebytes ().
They and operationsRaw Data
The difference between the two is: readbytes () First read the length of readable data to a q_uint32, and then read the number of bytes
Char *; writebytes () that has pre-allocated space write a q_unit32 containing the data length, and then the data. Note the encoding/Decoding of any data (except for the length
Q_uint32) must be done by yourself.

 

You can also refer to qtextstream, qvariant, input/output, and network.

Member type document
Qdatastream: byteorder

The byte sequence used to read/write data.

  • Qdatastream: bigendian
    -The default value is "high)
  • Qdatastream: littleendian
    -Low Level front
Member function documentation
Qdatastream: qdatastream ()

Construct a data stream without I/O devices.

 

You can also refer to setdevice ().


Qdatastream: qdatastream (
Qiodevice * d
)

Construct an IO DeviceD
.

Warning:
If you use qsocket or qsocketdevice
As an IO DeviceD
To ensure successful execution of the operation, you must ensure that sufficient data is provided on the socket. qdatastream has no way to read data.
Processing and restoring the read location is missing.

 

You can also refer to setdevice ()
And device ().


Qdatastream: qdatastream (
Qbytearray,
Int Mode)

Construct a qbuffer
To operate a byte arrayA
.Mode
That is, qiodevice: mode (),
Usually it is not io_readonly
It is io_writeonly.

Instance:

Static char bindata [] = {231, 1, 44 ,...};
Qbytearray;
A. setrawdata (bindata, sizeof (bindata); // A points to bindata
Qdatastream S (A, io_readonly); // open data of
S> [something]; // read the original bindata
A. resetrawdata (bindata, sizeof (bindata); // complete

Qbytearray: setrawdata ()
Functions are not provided to inexperienced people.


Qdatastream ::~ Qdatastream
() [Virtual]

Destroys the data stream.

The Destructor has no effect on the current IO Device unless it isConstructor
An internal Io passed to process qbytearray
In this case, the internal IO Device is destroyed ..

Bool
Qdatastream: atend () const

If the IO Device has reached the end point (the end point of a stream or file), or if no IO Device is set, true is returned. Otherwise, false is returned. For example, if the current position of the IO Device is before the end point.

 

You can also refer to qiodevice: atend ().

Int
Qdatastream: byteorder ()
Const

Returns the Current byte sequence settings -- not bigendian
Yes littleendian

.

 

You can also refer to setbyteorder ().

Qiodevice *
Qdatastream: Device () const

Returns the current set Io device.

You can also refer to setdevice ()
And unsetdevice ().

Bool
Qdatastream: EOF () const

This function is discarded.
It provides only to ensure that the old code can work. We strongly recommend that you do not use it in the new Code.

Returns true if the IO Device reaches the end point (the end point of a stream or file), or if no IO Device is set.

If the current IO read/write position is before the end, false is returned.

 

You can also refer to qiodevice: atend ().

Bool
Qdatastream: isprintabledata
() Const

Returns true if the printable data mark has been set.

You can also refer to setprintabledata ().

Qdatastream &

Qdatastream: Operator <(q_int8 I)

Write a signed byteI

To the stream and return the reference of the stream.

Qdatastream &

Qdatastream: Operator <(
Q_uint8 I)

This is an overloaded member function that provides convenience. Its behavior is basically the same as that of the above function.

Write an unsigned byteI
To the stream and return the reference of the stream.

Qdatastream &

Qdatastream: Operator <(
Q_int16 I)

This is an overloaded member function that provides convenience. Its behavior is basically the same as that of the above function.

Write a signed 16-digit integerI
To the stream and return the reference of the stream.

Qdatastream &

Qdatastream: Operator <(
Q_uint16 I)

This is an overloaded member function that provides convenience. Its behavior is basically the same as that of the above function.

Write an unsigned 16-digit integerI
To the stream and return the reference of the stream.

Qdatastream &

Qdatastream: Operator <(
Q_int32 I)

This is an overloaded member function that provides convenience. Its behavior is basically the same as that of the above function.

Write a signed 32-bit integerI
To the stream and return the reference of the stream.

Qdatastream &

Qdatastream: Operator <(
Q_uint32 I)

This is an overloaded member function that provides convenience. Its behavior is basically the same as that of the above function.

Write an unsigned 32-bit integerI
To the stream and return the reference of the stream.

Qdatastream &

Qdatastream: Operator <(q_long I
)

This is an overloaded member function that provides convenience. Its behavior is basically the same as that of the above function.

Write a signed integer with the length of the system word.I
To the stream and return the reference of the stream.

Qdatastream &

Qdatastream: Operator <(
Q_ulong I)

This is an overloaded member function that provides convenience. Its behavior is basically the same as that of the above function.

Write an unsigned integer with the length of the system word.I
To the stream and return the reference of the stream.

Qdatastream &

Qdatastream: Operator <(float F)

This is an overloaded member function that provides convenience. Its behavior is basically the same as that of the above function.

Write a 32-bit floating point using the standard ieee754 formatF
To the stream and return the reference of the stream.

Qdatastream &

Qdatastream: Operator <(double F
)

This is an overloaded member function that provides convenience. Its behavior is basically the same as that of the above function.

Write a 64-bit floating point using the standard ieee754 formatF
To the stream and return the reference of the stream.

Qdatastream &

Qdatastream: Operator <(
Const char * s)

This is an overloaded member function that provides convenience. Its behavior is basically the same as that of the above function.

Write a string ending with "/0"S
To the stream and return the reference of the stream.

This string uses writebytes ()
Serialized.

Qdatastream &

Qdatastream: Operator> (
Q_int8 & I)

Read a signed byte from the streamI

And return the stream reference.

Qdatastream &

Qdatastream: Operator> (
Q_uint8 & I)

This is an overloaded member function that provides convenience. Its behavior is basically the same as that of the above function.

Read an unsigned byte from the streamI
And return the stream reference.

Qdatastream &

Qdatastream: Operator> (
Q_int16 & I)

This is an overloaded member function that provides convenience. Its behavior is basically the same as that of the above function.

Read a signed 16-bit integer from the streamI
And return the stream reference.

Qdatastream &

Qdatastream: Operator> (
Q_uint16 & I)

This is an overloaded member function that provides convenience. Its behavior is basically the same as that of the above function.

Read an unsigned 16-bit integer from the streamI
And return the stream reference.

Qdatastream &

Qdatastream: Operator> (
Q_int32 & I)

This is an overloaded member function that provides convenience. Its behavior is basically the same as that of the above function.

Read a signed 32-bit integer from the streamI
And return the stream reference.

Qdatastream &

Qdatastream: Operator> (
Q_uint32 & I)

This is an overloaded member function that provides convenience. Its behavior is basically the same as that of the above function.

Read an unsigned 32-bit integer from the streamI
And return the stream reference.

Qdatastream &

Qdatastream: Operator> (
Q_long & I)

This is an overloaded member function that provides convenience. Its behavior is basically the same as that of the above function.

Read a signed integer from the stream with the length of the system wordI
And return the stream reference.

Qdatastream &

Qdatastream: Operator> (
Q_ulong & I)

This is an overloaded member function that provides convenience. Its behavior is basically the same as that of the above function.

Read an unsigned string from the stream. The length is an integer of the system word length.I
And return the stream reference.

Qdatastream &

Qdatastream: Operator> (
Float & F)

This is an overloaded member function that provides convenience. Its behavior is basically the same as that of the above function.

Read a 32-bit floating point number in the standard ieee754 format from the streamF
And return the stream reference.

Qdatastream &

Qdatastream: Operator> (
Double & F)

This is an overloaded member function that provides convenience. Its behavior is basically the same as that of the above function.

Read a 64-bit floating point number in the standard ieee754 format from the streamF
And return the stream reference.

Qdatastream &

Qdatastream: Operator> (
Char * & S)

This is an overloaded member function that provides convenience. Its behavior is basically the same as that of the above function.

Reads a string ending with "/0" from the streamS
And return the stream reference.

The space used to store this string isNew
To allocate -- the caller must useDelete []
To destroy it.

Qdatastream &

Qdatastream: readbytes (char * & S,
Uint & L)

Read cache from streamS
And return the stream reference.

This cacheS
YesNew
. Need to useDelete []
To destroy it. If the length is 0 orS
No
Can be allocated,S
It is set to 0.

L
The parameter is set to the cache length.

The serialized format is first a description of the q_uint32 length, and thenL
Bytes of data. Note dataNo
Encoding.

 

You can also refer to readrawbytes ()
And writebytes ().

Qdatastream &

Qdatastream: readrawbytes (char * s,
Uint Len)

Read from streamLen
ByteS
And return the stream reference.

This cacheS
Must be pre-allocated. DataNo
Encoding.

 

You can also refer to readbytes (), qiodevice: readblock ()
And writerawbytes ().

Void
Qdatastream: setbyteorder
(INT Bo)

Set the serialized byte sequenceBo
.

Bo
The parameter can be qdatastream: bigendian or qdatastream: littleendian.

The default value is high byte. We strongly recommend that you keep this setting unless you have special needs.

 

You can also refer to byteorder ().

Void
Qdatastream: setdevice (
Qiodevice * d
)

Set the IO DeviceD

.

You can also refer to device ()
And unsetdevice ().

Void
Qdatastream: setprintabledata
(Bool enable)

Set (ifEnable
True) or clear the printable data mark.

If this flag is set, the write function generates an output of printable characters (7-bit ASCII code.

We recommend that you enable the printable data setting only during debugging (it is slow and generates more output ).

Void
Qdatastream: setversion
(Int v)

Sets the version number of the serialized data format.

If you only use QT of the current version, you do not need to set this version number.

To provide new functions, some QT data streams in some versions have changed their serialized formats. If you want to read the data created in earlier versions of QT, or write programs that can be compiled by earlier versions of QT
Use this function to set the serial format of qdatastream.

 

  • To be compatible with QT 3.0, useV
    = 4.
  • To be compatible with QT 2.1.x and QT 2.2.x, useV
    = 3.
  • To be compatible with QT 2.0.x, useV
    = 2.
  • To be compatible with QT 1.x, useV
    = 1.

 

You can also refer to version ().

Void
Qdatastream: unsetdevice
()

Clear the IO Device. And call setdevice (
0.

You can also refer to device ()
And setdevice ().

Int
Qdatastream: Version ()
Const

Returns the version number of the serialized data format.
In QT 3.0, the version number is 4.

You can also refer to setversion ().

Qdatastream &

Qdatastream: writebytes (const char * s,
Uint Len)

Description of write LengthLen
And CacheS
To the stream and return the reference of the stream.

Len
Serialized as a q_uint32, followedS
InLen
Bytes. Note dataNo
Yes

Encoding.

 

You can also refer to writerawbytes ()
And readbytes ().

Qdatastream &

Qdatastream: writerawbytes (
Const char * s, uint Len)

SlaveS
Write inLen
Byte to the stream and return the reference of the stream. DataNo
Encoding.

 

You can also refer to writebytes (), qiodevice: writeblock ()
And readrawbytes ().

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.