Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |

# TQTextStream Class Reference

The TQTextStream class provides basic functions for reading and writing text using a TQIODevice. More...

All the functions in this class are reentrant when TQt is built with thread support.

`#include <ntqtextstream.h>`

Inherited by TQTextIStream and TQTextOStream.

List of all member functions.

## Public Members

- enum Encoding { Locale, Latin1, Unicode, UnicodeNetworkOrder, UnicodeReverse, RawUnicode, UnicodeUTF8 }
- void setEncoding ( Encoding e )
- void setCodec ( TQTextCodec * codec )
- TQTextCodec * codec ()
- TQTextStream ()
- TQTextStream ( TQIODevice * iod )
- TQTextStream ( TQString * str, int filemode )
- TQTextStream ( TQString & str, int filemode ) (obsolete)
- TQTextStream ( TQByteArray a, int mode )
- TQTextStream ( FILE * fh, int mode )
- virtual ~TQTextStream ()
- TQIODevice * device () const
- void setDevice ( TQIODevice * iod )
- void unsetDevice ()
- bool atEnd () const
- bool eof () const (obsolete)
- TQTextStream & operator>> ( TQChar & c )
- TQTextStream & operator>> ( char & c )
- TQTextStream & operator>> ( signed short & i )
- TQTextStream & operator>> ( unsigned short & i )
- TQTextStream & operator>> ( signed int & i )
- TQTextStream & operator>> ( unsigned int & i )
- TQTextStream & operator>> ( signed long & i )
- TQTextStream & operator>> ( unsigned long & i )
- TQTextStream & operator>> ( float & f )
- TQTextStream & operator>> ( double & f )
- TQTextStream & operator>> ( char * s )
- TQTextStream & operator>> ( TQString & str )
- TQTextStream & operator>> ( TQCString & str )
- TQTextStream & operator<< ( TQChar c )
- TQTextStream & operator<< ( char c )
- TQTextStream & operator<< ( signed short i )
- TQTextStream & operator<< ( unsigned short i )
- TQTextStream & operator<< ( signed int i )
- TQTextStream & operator<< ( unsigned int i )
- TQTextStream & operator<< ( signed long i )
- TQTextStream & operator<< ( unsigned long i )
- TQTextStream & operator<< ( float f )
- TQTextStream & operator<< ( double f )
- TQTextStream & operator<< ( const char * s )
- TQTextStream & operator<< ( const TQString & s )
- TQTextStream & operator<< ( const TQCString & s )
- TQTextStream & operator<< ( void * ptr )
- TQTextStream & readRawBytes ( char * s, uint len )
- TQTextStream & writeRawBytes ( const char * s, uint len )
- TQString readLine ()
- TQString read ()
- void skipWhiteSpace ()
- 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 w )
- int fill () const
- int fill ( int f )
- int precision () const
- int precision ( int p )

## Detailed Description

The TQTextStream class provides basic functions for reading and writing text using a TQIODevice.

The text stream class has a functional interface that is very similar to that of the standard C++ iostream class.

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

Function | Meaning
--- | ---
bin | sets the TQTextStream to read/write binary numbers
oct | sets the TQTextStream to read/write octal numbers
dec | sets the TQTextStream to read/write decimal numbers
hex | sets the TQTextStream to read/write hexadecimal numbers
endl | forces a line break
flush | forces the TQIODevice to flush any buffered data
ws | eats any available whitespace (on input)
reset | resets the TQTextStream to its default mode (see reset())
qSetW(int) | sets the field width to the given argument
qSetFill(int) | sets the fill character to the given argument
qSetPrecision(int) | sets the precision to the given argument

Warning: By default TQTextStream 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, i.e. the sequence "0100" will be interpreted as 64.

The TQTextStream class reads and writes text; it is not appropriate for dealing with binary data (but TQDataStream is).

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

The TQIODevice is set in the constructor, or later using setDevice(). If the end of the input is reached atEnd() returns TRUE. Data can be read into variables of the appropriate type using the operator>>() overloads, or read in its entirety into a single string using read(), or read a line at a time using readLine(). Whitespace can be skipped over using skipWhiteSpace(). You can set flags for the stream using flags() or setf(). The stream also supports width(), precision() and fill(); use reset() to reset the defaults.

See also TQDataStream, Input/Output and Networking, and Text Related Classes.

## Member Type Documentation

### TQTextStream::Encoding

- `TQTextStream::Locale`
- `TQTextStream::Latin1`
- `TQTextStream::Unicode`
- `TQTextStream::UnicodeNetworkOrder`
- `TQTextStream::UnicodeReverse`
- `TQTextStream::RawUnicode`
- `TQTextStream::UnicodeUTF8`

See setEncoding() for an explanation of the encodings.

## Member Function Documentation

### TQTextStream::TQTextStream ()

Constructs a data stream that has no IO device.

### TQTextStream::TQTextStream ( TQIODevice * iod )

Constructs a text stream that uses the IO device iod.

### TQTextStream::TQTextStream ( TQString * str, int filemode )

Constructs a text stream that operates on the Unicode TQString, str, through an internal device. The filemode argument is passed to the device's open() function; see TQIODevice::mode().

If you set an encoding or codec with setEncoding() or setCodec(), this setting is ignored for text streams that operate on TQString.

Example:

        TQString str;
        TQTextStream 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 the string will not be truncated:

        TQString str = "pi = 3.14";
        TQTextStream ts( &str, IO_WriteOnly );
        ts <<  "2+2 = " << 2+2; // str == "2+2 = 414"

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

### TQTextStream::TQTextStream ( TQString & str, int filemode )

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

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

### TQTextStream::TQTextStream ( TQByteArray a, int mode )

Constructs a text stream that operates on the byte array, a, through an internal TQBuffer device. The mode argument is passed to the device's open() function; see TQIODevice::mode().

Example:

        TQByteArray array;
        TQTextStream 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 TQBuffer:

        TQByteArray array;
        TQBuffer buf( array );
        buf.open( IO_WriteOnly );
        TQTextStream ts( &buf );
        ts << "pi = " << 3.14 << '\0'; // array == "pi = 3.14"
        buf.close();

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

Constructs a text stream that operates on an existing file handle fh through an internal TQFile device. The mode argument is passed to the device's open() function; see TQIODevice::mode().

Note that if you create a TQTextStream `cout` or another name that is also used for another variable of a different type, some linkers may confuse the two variables, which will often cause crashes.

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

Destroys the text stream.

The destructor does not affect the current IO device.

### bool TQTextStream::atEnd () const

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

See also TQIODevice::atEnd().

Examples: addressbook/centralwidget.cpp and grapher/grapher.cpp.

### TQTextCodec * TQTextStream::codec ()

Returns the codec actually used for this stream.

If Unicode is automatically detected in input, a codec with name() "ISO-10646-UCS-2" is returned.

See also setCodec().

### TQIODevice * TQTextStream::device () const

Returns the IO device currently set.

See also setDevice() and unsetDevice().

### bool TQTextStream::eof () const

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

This function has been renamed to atEnd().

See also TQIODevice::atEnd().

Example: chart/chartform_files.cpp.

### int TQTextStream::fill () const

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

### int TQTextStream::fill ( int f )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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

### int TQTextStream::flags () const

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

Flag | Meaning
--- | ---
`skipws` | Not currently used; whitespace always skipped
`left` | Numeric fields are left-aligned
`right` | Not currently used (by default, numerics are right-aligned)
`internal` | Puts 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` | Annotates numeric outputs with 0b, 0, or 0x if in `bin`, `oct`, or `hex` format
`showpoint` | Not currently used
`uppercase` | Uses 0B and 0X rather than 0b and 0x
`showpos` | Shows + for positive numeric values
`scientific` | Uses scientific notation for floating point values
`fixed` | Uses 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 it starts with 0b, and decimal otherwise.

See also setf() and unsetf().

### int TQTextStream::flags ( int f )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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

See also setf() and unsetf().

### TQTextStream & TQTextStream::operator<< ( TQChar c )

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

The character c is assumed to be Latin1 encoded independent of the Encoding set for the TQTextStream.

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

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Writes character c to the stream and returns a reference to the stream.

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

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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

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

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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

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

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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

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

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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

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

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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

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

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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

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

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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

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

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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

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

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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

The string s is assumed to be Latin1 encoded independent of the Encoding set for the TQTextStream.

### TQTextStream & TQTextStream::operator<< ( const TQString & s )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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

### TQTextStream & TQTextStream::operator<< ( const TQCString & s )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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

The string s is assumed to be Latin1 encoded independent of the Encoding set for the TQTextStream.

### TQTextStream & TQTextStream::operator<< ( void * ptr )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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

The ptr is output as an unsigned long hexadecimal integer.

### TQTextStream & TQTextStream::operator>> ( TQChar & c )

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

### TQTextStream & TQTextStream::operator>> ( char & c )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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

### TQTextStream & TQTextStream::operator>> ( signed short & i )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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

### TQTextStream & TQTextStream::operator>> ( unsigned short & i )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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

### TQTextStream & TQTextStream::operator>> ( signed int & i )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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

### TQTextStream & TQTextStream::operator>> ( unsigned int & i )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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

### TQTextStream & TQTextStream::operator>> ( signed long & i )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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

### TQTextStream & TQTextStream::operator>> ( unsigned long & i )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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

### TQTextStream & TQTextStream::operator>> ( float & f )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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

### TQTextStream & TQTextStream::operator>> ( double & f )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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

### TQTextStream & TQTextStream::operator>> ( char * s )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Reads a "word" from the stream into s and returns a reference to the stream.

A word consists of characters for which isspace() returns FALSE.

### TQTextStream & TQTextStream::operator>> ( TQString & str )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Reads a "word" from the stream into str and returns a reference to the stream.

A word consists of characters for which isspace() returns FALSE.

### TQTextStream & TQTextStream::operator>> ( TQCString & str )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Reads a "word" from the stream into str and returns a reference to the stream.

A word consists of characters for which isspace() returns FALSE.

### int TQTextStream::precision () const

Returns the precision. The default value is 6.

### int TQTextStream::precision ( int p )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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

### TQString TQTextStream::read ()

Reads the entire stream from the current position, and returns a string containing the text.

See also TQIODevice::readLine().

Examples: action/application.cpp, application/application.cpp, mdi/application.cpp, qdir/qdir.cpp, and qwerty/qwerty.cpp.

### TQString TQTextStream::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 TQIODevice::readLine(), which does not strip the newline at the end of the line.

On EOF you will get a TQString that is null. On reading an empty line the returned TQString is empty but not null.

See also TQIODevice::readLine().

Examples: addressbook/centralwidget.cpp, chart/element.cpp, and network/clientserver/server/server.cpp.

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

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

The buffer s must be preallocated.

Note that no encoding is done by this function.

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

See also TQIODevice::readBlock().

### void TQTextStream::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 TQTextStream::setCodec ( TQTextCodec * codec )

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

Note that this function should be called before any data is read to/written from the stream.

See also setEncoding() and codec().

Example: qwerty/qwerty.cpp.

### void TQTextStream::setDevice ( TQIODevice * iod )

Sets the IO device to iod.

See also device() and unsetDevice().

### void TQTextStream::setEncoding ( Encoding e )

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

Encoding | Meaning
--- | ---
Locale | Uses local file format (Latin1 if locale is not set), but autodetecting Unicode(utf16) on input.
Unicode | Uses Unicode(utf16) for input and output. Output will be written in the order most efficient for the current platform (i.e. the order used internally in TQString).
UnicodeUTF8 | Using Unicode(utf8) for input and output. If you use it for input it will autodetect utf16 and use it instead of utf8.
Latin1 | ISO-8859-1. Will not autodetect utf16.
UnicodeNetworkOrder | Uses network order Unicode(utf16) for input and output. Useful when reading Unicode data that does not start with the byte order marker.
UnicodeReverse | Uses reverse network order Unicode(utf16) for input and output. Useful when reading Unicode data that does not start with the byte order marker or when writing data that should be read by buggy Windows applications.
RawUnicode | Like Unicode, but does not write the byte order marker nor does it auto-detect the byte order. Useful only when writing to non-persistent storage used by a single process.

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

Note that this function should be called before any data is read to or written from the stream.

See also setCodec().

Examples: addressbook/centralwidget.cpp, network/httpd/httpd.cpp, and qwerty/qwerty.cpp.

### int TQTextStream::setf ( int bits )

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

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

See also unsetf().

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

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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

### void TQTextStream::skipWhiteSpace ()

Positions the read pointer at the first non-whitespace character.

### void TQTextStream::unsetDevice ()

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

See also device() and setDevice().

### int TQTextStream::unsetf ( int bits )

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

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

See also setf().

### int TQTextStream::width () const

Returns the field width. The default value is 0.

### int TQTextStream::width ( int w )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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

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

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

Note that no encoding is done by this function.

See also TQIODevice::writeBlock().

This file is part of the TQt toolkit. Copyright © 1995-2007 Trolltech. All Rights Reserved.