Qt Toolkit - QTextStream Class

# QTextStream Class Reference

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

`#include <qtextstream.h>`

List of all member functions.

## Public Members

- QTextStream ()
- QTextStream ( QIODevice * )
- QTextStream ( QByteArray, int mode )
- QTextStream ( FILE *, int mode )
- virtual ~QTextStream ()
- QIODevice* device () const
- void setDevice ( QIODevice * )
- void unsetDevice ()
- bool eof () const
- QTextStream& operator>> ( char & )
- QTextStream& operator>> ( signed short & )
- QTextStream& operator>> ( unsigned short & )
- QTextStream& operator>> ( signed int & )
- QTextStream& operator>> ( unsigned int & )
- QTextStream& operator>> ( signed long & )
- QTextStream& operator>> ( unsigned long & )
- QTextStream& operator>> ( float & )
- QTextStream& operator>> ( double & )
- QTextStream& operator>> ( char * )
- QTextStream& operator>> ( QString & )
- QTextStream& operator<< ( char )
- QTextStream& operator<< ( signed short )
- QTextStream& operator<< ( unsigned short )
- QTextStream& operator<< ( signed int )
- QTextStream& operator<< ( unsigned int )
- QTextStream& operator<< ( signed long )
- QTextStream& operator<< ( unsigned long )
- QTextStream& operator<< ( float )
- QTextStream& operator<< ( double )
- QTextStream& operator<< ( const char * )
- QTextStream& operator<< ( void * )
- QTextStream& readRawBytes ( char *, uint len )
- QTextStream& writeRawBytes ( const char *, uint len )
- QString readLine ()
- enum { skipws = 0x0001, left = 0x0002, right = 0x0004, internal = 0x0008, bin = 0x0010, oct = 0x0020, dec = 0x0040, hex = 0x0080, showbase = 0x0100, showpoint = 0x0200, uppercase = 0x0400, showpos = 0x0800, scientific= 0x1000, fixed = 0x2000 }
- int flags () const
- int flags ( int f )
- int setf ( int bits )
- int setf ( int bits, int mask )
- int unsetf ( int bits )
- void reset ()
- int width () const
- int width ( int )
- int fill () const
- int fill ( int )
- int precision () const
- int precision ( int )

## 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:

- `bin` sets the QTextStream to output binary numbers
- `oct` sets the QTextStream to output octal numbers
- `dec` sets the QTextStream to output decimal numbers
- `hex` sets the QTextStream to output hexadecimal numbers
- `endl` forces a line break
- `flush` forces the QIODevice to flush any buffered data
- `ws` eats any available white space (on input)
- `reset` resets the QTextStream to its default mode (see reset()).

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

See also: QDataStream.

Examples: grapher/grapher.cpp application/application.cpp

## Member Function Documentation

### QTextStream::QTextStream ()

Constructs a data stream that has no IO device.

### QTextStream::QTextStream ( QByteArray a, int mode )

Constructs a text stream that operates on a byte array throught an internal QBuffer 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.

Same example, using a QBuffer:

        QString str;
        QBuffer buf( str );
        buf.open( IO_WriteOnly );
        QTextStream ts( &buf );
        ts << "pi = " << 3.14;                      // str == "pi = 3.14"
        buf.close();

Note that QStrings created in this way will not have NUL terminators. So unless you are using the string purely as an array of bytes, you should terminate them:

        QString str;
        ...
        ts << "pi = " << 3.14;                      // str == "pi = 3.14"
        ts << '\0';

### QTextStream::QTextStream ( QIODevice * d )

Constructs a text stream that uses the IO device d.

### QTextStream::QTextStream ( FILE * fh, int mode )

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

Example:

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

### QTextStream::~QTextStream () `[virtual]`

Destroys the text stream.

The destructor will not affect the current IO device.

### QIODevice * QTextStream::device () const

Returns the IO device currently set.

See also: setDevice() and unsetDevice().

### bool QTextStream::eof () 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().

Examples: grapher/grapher.cpp application/application.cpp

### int QTextStream::fill () const

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

### int QTextStream::fill ( int f )

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

### int QTextStream::flags () const

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

The meaning of the flags are:

- skipws - Not currently used - whitespace always skipped
- left - Numeric fields are left-aligned
- right - Not currently used (by default numerics are right aligned)
- internal - Put any padding spaces between +/- and value
- bin - Output and input only in binary
- oct - Output and input only in octal
- dec - Output and input only in decimal
- hex - Output and input only in hexadecimal
- showbase - Annotate numeric outputs with 0b, 0, or 0x if in bin, oct, or hex format
- showpoint - Not currently used
- uppercase - Use 0B and 0X rather than 0b and 0x
- showpos - Show + for positive numeric values
- scientific - Use scientific notation for floating point values
- fixed - Use fixed-point notation for floating point values

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().

### int QTextStream::flags ( int f )

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

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

### QTextStream & QTextStream::operator<< ( char c )

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

### QTextStream & QTextStream::operator<< ( const char * s )

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

### QTextStream & QTextStream::operator<< ( double f )

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

### QTextStream & QTextStream::operator<< ( float f )

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

### QTextStream & QTextStream::operator<< ( signed int i )

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

### QTextStream & QTextStream::operator<< ( signed long i )

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

### QTextStream & QTextStream::operator<< ( signed short i )

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

### QTextStream & QTextStream::operator<< ( unsigned int i )

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

### QTextStream & QTextStream::operator<< ( unsigned long i )

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

### QTextStream & QTextStream::operator<< ( unsigned short i )

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>> ( 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.

### 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>> ( signed int & 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>> ( signed long & 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>> ( signed short & 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>> ( unsigned int & 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>> ( unsigned long & 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>> ( unsigned short & 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.

### int QTextStream::precision () const

Returns the precision. The default value is 6.

### int QTextStream::precision ( int p )

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

### QString QTextStream::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().

Examples: application/application.cpp

### QTextStream & QTextStream::readRawBytes ( char * s, uint len )

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

The buffer s must be preallocated.

See also: QIODevice::readBlock().

### void QTextStream::reset ()

Resets the text stream.

- All flags are set to 0.
- The field width is set to 0.
- The fill character is set to ' ' (space).
- The precision is set to 6.

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

### void QTextStream::setDevice ( QIODevice * d )

Sets the IO device to d.

See also: device() and unsetDevice().

### int QTextStream::setf ( int bits )

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

Equivalent to `flags( flags() | bits )`.

See also: setf() and unsetf().

### int QTextStream::setf ( int bits, int mask )

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().

### void QTextStream::unsetDevice ()

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

See also: device() and setDevice().

### int QTextStream::unsetf ( int bits )

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

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

See also: setf().

### int QTextStream::width () const

Returns the field width. The default value is 0.

### int QTextStream::width ( int w )

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

### QTextStream & QTextStream::writeRawBytes ( const char * s, uint len )

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

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.

It was generated from the following files:

- qtextstream.h: 1998/08/25
- qtextstream.cpp: 1998/07/03