Qt logo

QTextStream Class Reference


The QTextStream class provides basic functions for reading and writing text using a QIODevice. More...

#include <qtextstream.h>

Inherited by QTextIStream and QTextOStream.

List of all member functions.

Public Members


Detailed Description

The QTextStream class provides basic functions for reading and writing text using a QIODevice.

The text stream class has a functional interface that is very similar to that of the standard C++ iostream class. The difference between iostream and QTextStream is that our stream operates on a QIODevice, which is easily subclassed, while iostream operates on FILE * pointers, which can not be subclassed.

Qt provides several global functions similar to the ones in iostream:

Warning: By default, QTextStream will automatically detect whether integers in the stream are in decimal, octal, hexadecimal or binary format when reading from the stream. In particular, a leading '0' signifies octal, ie. the sequence "0100" will be interpreted as 64.

The QTextStream class reads and writes text and it is not appropriate for dealing with binary data (but QDataStream is).

By default output of Unicode text (ie. QString) is done using the local 8-bit encoding. This can be changed using the setEncoding() method. For input, the QTextStream will auto-detect standard Unicode "byte order marked" text files, but otherwise the local 8-bit encoding is used.

See also: QDataStream.

Examples: grapher/grapher.cpp


Member Function Documentation

QTextStream::QTextStream()

Constructs a data stream that has no IO device.

QTextStream::QTextStream(QIODevice*d)

Constructs a text stream that uses the IO device d.

QTextStream::QTextStream(QString&str, intfilemode)

This function is obsolete. It is provided to keep old programs working. We strongly advise against using it in new code.

This constructor is equivalent to the constructor taking a QString* parameter.

QTextStream::QTextStream(QString*str, intfilemode)

Constructs a text stream that operates on a Unicode QString through an internal device.

Example:

    QString str;
    QTextStream ts( &str, IO_WriteOnly );
    ts << "pi = " << 3.14;                      // str == "pi = 3.14..."

Writing data to the text stream will modify the contents of the string. The string will be expanded when data is written beyond the end of the string.

Note that since QString is Unicode, you should not use readRawBytes() or writeRawBytes() on such a stream.

QTextStream::QTextStream(FILE*fh, intmode)

Constructs a text stream that operates on an existing file handle fh through an internal QFile device.

Example:

    QTextStream cout( stdout, IO_WriteOnly );
    QTextStream cin ( stdin,  IO_ReadOnly );
    QTextStream cerr( stderr, IO_WriteOnly );

QTextStream::QTextStream(QByteArraya, intmode)

Constructs a text stream that operates on a byte array through an internal QBuffer device.

Example:

    QByteArray array;
    QTextStream ts( array, IO_WriteOnly );
    ts << "pi = " << 3.14 << '\0';              // array == "pi = 3.14"

Writing data to the text stream will modify the contents of the array. The array will be expanded when data is written beyond the end of the string.

Same example, using a QBuffer:

    QByteArray array;
    QBuffer buf( array );
    buf.open( IO_WriteOnly );
    QTextStream ts( &buf );
    ts << "pi = " << 3.14 << '\0';              // array == "pi = 3.14"
    buf.close();

QTextStream::~QTextStream() [virtual]

Destroys the text stream.

The destructor does not affect the current IO device.

boolQTextStream::atEnd()const

Returns TRUE if the IO device has reached the end position (end of stream or file) or if there is no IO device set.

Returns FALSE if the current position of the read/write head of the IO device is somewhere before the end position.

See also: QIODevice::atEnd().

QIODevice*QTextStream::device()const

Returns the IO device currently set.

See also: setDevice() and unsetDevice().

boolQTextStream::eof()const

This function is obsolete. It is provided to keep old programs working. We strongly advise against using it in new code.

This function has been renamed to atEnd().

See also: QIODevice::atEnd().

Examples: grapher/grapher.cpp

intQTextStream::fill()const

Returns the fill character. The default value is ' ' (space).

intQTextStream::fill(intf)

Sets the fill character to f. Returns the previous fill character.

intQTextStream::flags()const

Returns the current stream flags. The default value is 0.

The meaning of the flags are:

Note that unless bin, oct, dec, or hex is set, the input base is octal if the value starts with 0, hexadecimal if it starts with 0x, binary if the value starts with 0b, and decimal otherwise.

See also: setf() and unsetf().

intQTextStream::flags(intf)

Sets the stream flags to f. Returns the previous stream flags.

See also: setf(), unsetf() and flags().

QTextStream&QTextStream::operator<<(charc)

Writes a char to the stream and returns a reference to the stream.

QTextStream&QTextStream::operator<<(constQString&s)

Writes a string to the stream and returns a reference to the stream.

QTextStream&QTextStream::operator<<(constchar*s)

Writes a string to the stream and returns a reference to the stream.

QTextStream&QTextStream::operator<<(doublef)

Writes a double to the stream and returns a reference to the stream.

QTextStream&QTextStream::operator<<(floatf)

Writes a float to the stream and returns a reference to the stream.

QTextStream&QTextStream::operator<<(signedinti)

Writes an int to the stream and returns a reference to the stream.

QTextStream&QTextStream::operator<<(signedlongi)

Writes a long int to the stream and returns a reference to the stream.

QTextStream&QTextStream::operator<<(signedshorti)

Writes a short integer to the stream and returns a reference to the stream.

QTextStream&QTextStream::operator<<(unsignedinti)

Writes an unsigned int to the stream and returns a reference to the stream.

QTextStream&QTextStream::operator<<(unsignedlongi)

Writes an unsigned long int to the stream and returns a reference to the stream.

QTextStream&QTextStream::operator<<(unsignedshorti)

Writes an unsigned short integer to the stream and returns a reference to the stream.

QTextStream&QTextStream::operator<<(void*ptr)

Writes a pointer to the stream and returns a reference to the stream.

The ptr is output as an unsigned long hexadecimal integer.

QTextStream&QTextStream::operator>>(QChar&c)

Reads a char from the stream and returns a reference to the stream. Note that whitespace is \em not skipped.

QTextStream&QTextStream::operator>>(QCString&str)

Reads a word from the stream and returns a reference to the stream.

QTextStream&QTextStream::operator>>(QString&str)

Reads a word from the stream and returns a reference to the stream.

QTextStream&QTextStream::operator>>(char&c)

Reads a char from the stream and returns a reference to the stream. Note that whitespace is skipped.

QTextStream&QTextStream::operator>>(char*s)

Reads a word from the stream and returns a reference to the stream.

QTextStream&QTextStream::operator>>(double&f)

Reads a double from the stream and returns a reference to the stream. See flags() for an explanation of expected input format.

QTextStream&QTextStream::operator>>(float&f)

Reads a float from the stream and returns a reference to the stream. See flags() for an explanation of expected input format.

QTextStream&QTextStream::operator>>(signedint&i)

Reads a signed int from the stream and returns a reference to the stream. See flags() for an explanation of expected input format.

QTextStream&QTextStream::operator>>(signedlong&i)

Reads a signed long int from the stream and returns a reference to the stream. See flags() for an explanation of expected input format.

QTextStream&QTextStream::operator>>(signedshort&i)

Reads a signed short integer from the stream and returns a reference to the stream. See flags() for an explanation of expected input format.

QTextStream&QTextStream::operator>>(unsignedint&i)

Reads an unsigned int from the stream and returns a reference to the stream. See flags() for an explanation of expected input format.

QTextStream&QTextStream::operator>>(unsignedlong&i)

Reads an unsigned long int from the stream and returns a reference to the stream. See flags() for an explanation of expected input format.

QTextStream&QTextStream::operator>>(unsignedshort&i)

Reads an unsigned short integer from the stream and returns a reference to the stream. See flags() for an explanation of expected input format.

intQTextStream::precision()const

Returns the precision. The default value is 6.

intQTextStream::precision(intp)

Sets the precision to p. Returns the previous precision setting.

QStringQTextStream::read()

Reads the entire stream and returns a string containing the text.

See also: QIODevice::readLine().

QStringQTextStream::readLine()

Reads a line from the stream and returns a string containing the text.

The returned string does not contain any trailing newline or carriage return. Note that this is different from QIODevice::readLine(), which does not strip the newline at the end of the line.

See also: QIODevice::readLine().

QTextStream&QTextStream::readRawBytes(char*s, uintlen)

Reads len bytes from the stream into e s and returns a reference to the stream.

The buffer s must be preallocated.

\note No Encoding is done by this function.

Warning: The behaviour of this function is undefined unless the stream's encoding is set to Unicode or Latin1.

See also: QIODevice::readBlock().

voidQTextStream::reset()

Resets the text stream.

See also: setf(), width(), fill() and precision().

voidQTextStream::setCodec(QTextCodec*codec)

Sets the codec for this stream to codec. Will not try to autodetect Unicode.

\note This function should be called before any data is read to/written from the stream.

See also: setEncoding.

voidQTextStream::setDevice(QIODevice*d)

Sets the IO device to d.

See also: device() and unsetDevice().

voidQTextStream::setEncoding(Encodinge)

Sets the encoding of this stream to e, where e is one of:

Locale and all Unicode encodings, except RawUnicode, will look at the first two bytes in a input stream to determine the byte order. The initial byte order marker will be stripped off before data is read.

\note This function should be called before any data is read to/written from the stream.

See also: setCodec.

intQTextStream::setf(intbits)

Sets the stream flag bits bits. Returns the previous stream flags.

Equivalent to flags( flags() | bits ).

See also: setf() and unsetf().

intQTextStream::setf(intbits, intmask)

Sets the stream flag bits bits with a bit mask mask. Returns the previous stream flags.

Equivalent to flags( (flags() & ~mask) | (bits & mask) ).

See also: setf() and unsetf().

voidQTextStream::skipWhiteSpace()

Equivalent to *this << ws.

voidQTextStream::unsetDevice()

Unsets the IO device. Equivalent to setDevice( 0 ).

See also: device() and setDevice().

intQTextStream::unsetf(intbits)

Clears the stream flag bits bits. Returns the previous stream flags.

Equivalent to flags( flags() & ~mask ).

See also: setf().

intQTextStream::width()const

Returns the field width. The default value is 0.

intQTextStream::width(intw)

Sets the field width to w. Returns the previous field width.

QTextStream&QTextStream::writeRawBytes(constchar*s, uintlen)

Writes the len bytes from s to the stream and returns a reference to the stream.

\note No Encoding is done by this function.

See also: QIODevice::writeBlock().


Search the documentation, FAQ, qt-interest archive and more (uses www.troll.no):


This file is part of the Qt toolkit, copyright © 1995-99 Troll Tech, all rights reserved.


Copyright 1999 Troll TechTrademarks
Qt version 2.0.2