QTextStream(3qt)

NAME
QTextStream - Basic functions for reading and writing text using a QIODevice

SYNOPSIS
All the functions in this class are reentrant when Qt is built with thread support.</p>

#include <ntqtextstream.h>

Inherited by QTextIStream and QTextOStream.

  Public Members
enum Encoding { Locale, Latin1, Unicode, UnicodeNetworkOrder, UnicodeReverse, RawUnicode, UnicodeUTF8 }
void setEncoding ( Encoding e )
void setCodec ( QTextCodec * codec )
QTextCodec * codec ()
QTextStream ()
QTextStream ( QIODevice * iod )
QTextStream ( QString * str, int filemode )
QTextStream ( QString & str, int filemode ) (obsolete)
QTextStream ( QByteArray a, int mode )
QTextStream ( FILE * fh, int mode )
virtual ~QTextStream ()
QIODevice * device () const
void setDevice ( QIODevice * iod )
void unsetDevice ()
bool atEnd () const
bool eof () const (obsolete)
QTextStream & operator>> ( QChar & c )
QTextStream & operator>> ( char & c )
QTextStream & operator>> ( signed short & i )
QTextStream & operator>> ( unsigned short & i )
QTextStream & operator>> ( signed int & i )
QTextStream & operator>> ( unsigned int & i )
QTextStream & operator>> ( signed long & i )
QTextStream & operator>> ( unsigned long & i )
QTextStream & operator>> ( float & f )
QTextStream & operator>> ( double & f )
QTextStream & operator>> ( char * s )
QTextStream & operator>> ( QString & str )
QTextStream & operator>> ( QCString & str )
QTextStream & operator<< ( QChar c )
QTextStream & operator<< ( char c )
QTextStream & operator<< ( signed short i )
QTextStream & operator<< ( unsigned short i )
QTextStream & operator<< ( signed int i )
QTextStream & operator<< ( unsigned int i )
QTextStream & operator<< ( signed long i )
QTextStream & operator<< ( unsigned long i )
QTextStream & operator<< ( float f )
QTextStream & operator<< ( double f )
QTextStream & operator<< ( const char * s )
QTextStream & operator<< ( const QString & s )
QTextStream & operator<< ( const QCString & s )
QTextStream & operator<< ( void * ptr )
QTextStream & readRawBytes ( char * s, uint len )
QTextStream & writeRawBytes ( const char * s, uint len )
QString readLine ()
QString 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 )

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.

Qt provides several global functions similar to the ones in iostream: <center>.nf
</center>

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

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

By default, output of Unicode text (i.e. 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; otherwise the local 8-bit encoding is used.

The QIODevice 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 QDataStream, Input/Output and Networking, and Text Related Classes.

  Member Type Documentation

QTextStream::Encoding

QTextStream::Locale

QTextStream::Latin1

QTextStream::Unicode

QTextStream::UnicodeNetworkOrder

QTextStream::UnicodeReverse

QTextStream::RawUnicode

QTextStream::UnicodeUTF8

See setEncoding() for an explanation of the encodings.

MEMBER FUNCTION DOCUMENTATION

QTextStream::QTextStream ()
Constructs a data stream that has no IO device.

QTextStream::QTextStream ( QIODevice * iod )
Constructs a text stream that uses the IO device iod.

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

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

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 the string will not be truncated:

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

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

QTextStream::QTextStream ( QString & 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 QString* parameter.

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

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 ( FILE * fh, int mode )
Constructs a text stream that operates on an existing file handle fh through an internal QFile device. The mode argument is passed to the device's open() function; see QIODevice::mode().

Note that if you create a QTextStream 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.

QTextStream::~QTextStream () [virtual]
Destroys the text stream.

The destructor does not affect the current IO device.

bool QTextStream::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 QIODevice::atEnd().

Examples:

QTextCodec * QTextStream::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().

QIODevice * QTextStream::device () const
Returns the IO device currently set.

See also setDevice() and unsetDevice().

bool QTextStream::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 QIODevice::atEnd().

Example: chart/chartform_files.cpp.

int QTextStream::fill () const
Returns the fill character. The default value is ' ' (space).

int QTextStream::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 QTextStream::flags () const
Returns the current stream flags. The default value is 0.

<center>.nf
Flag | Meaning
 | Not currently used; whitespace always skipped
 | Numeric fields are left-aligned
 | Not currently used (by default, numerics are right-aligned)
 | Puts any padding spaces between +/- and value
 | Output and input only in binary
 | Output and input only in octal
 | Output and input only in decimal
 | Output and input only in hexadecimal
 | Annotates numeric outputs with 0b, 0, or 0x if in
 | Not currently used
 | Uses 0B and 0X rather than 0b and 0x
 | Shows + for positive numeric values
 | Uses scientific notation for floating point values
</center>

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

QTextStream & QTextStream::operator<< ( QChar 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 QTextStream.

QTextStream & QTextStream::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.

QTextStream & QTextStream::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.

QTextStream & QTextStream::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.

QTextStream & QTextStream::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.

QTextStream & QTextStream::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.

QTextStream & QTextStream::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.

QTextStream & QTextStream::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.

QTextStream & QTextStream::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.

QTextStream & QTextStream::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.

QTextStream & QTextStream::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 QTextStream.

QTextStream & QTextStream::operator<< ( const QString & 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.

QTextStream & QTextStream::operator<< ( const QCString & 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 QTextStream.

QTextStream & QTextStream::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.

QTextStream & QTextStream::operator>> ( QChar & c )
Reads a char c from the stream and returns a reference to the stream. Note that whitespace is not skipped.

QTextStream & QTextStream::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.

QTextStream & QTextStream::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.

QTextStream & QTextStream::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.

QTextStream & QTextStream::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.

QTextStream & QTextStream::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.

QTextStream & QTextStream::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.

QTextStream & QTextStream::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.

QTextStream & QTextStream::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.

QTextStream & QTextStream::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.

QTextStream & QTextStream::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.

QTextStream & QTextStream::operator>> ( QString & 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.

QTextStream & QTextStream::operator>> ( QCString & 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 QTextStream::precision () const
Returns the precision. The default value is 6.

int QTextStream::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.

QString QTextStream::read ()
Reads the entire stream from the current position, and returns a string containing the text.

See also QIODevice::readLine().

Examples:

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.

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

See also QIODevice::readLine().

Examples:

QTextStream & QTextStream::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 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::setCodec ( QTextCodec * 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 QTextStream::setDevice ( QIODevice * iod )
Sets the IO device to iod.

See also device() and unsetDevice().

void QTextStream::setEncoding ( Encoding e )
Sets the encoding of this stream to e, where e is one of the following values: <center>.nf
</center>

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:

int QTextStream::setf ( int bits )
Sets the stream flag bits bits. Returns the previous stream flags.

Equivalent to flags( flags() | bits ).

See also unsetf().

int QTextStream::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 QTextStream::skipWhiteSpace ()
Positions the read pointer at the first non-whitespace character.

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

QTextStream & QTextStream::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 QIODevice::writeBlock().

SEE ALSO
http://doc.trolltech.com/ntqtextstream.html http://www.trolltech.com/faq/tech.html

COPYRIGHT
Copyright 1992-2007 Trolltech ASA, http://www.trolltech.com. See the license file included in the distribution for a complete license statement.

AUTHOR
Generated automatically from the source code.

BUGS
If you find a bug in Qt, please report it as described in http://doc.trolltech.com/bughowto.html. Good bug reports help us to help you. Thank you.

The definitive Qt documentation is provided in HTML format; it is located at $QTDIR/doc/html and can be read using Qt Assistant or with a web browser. This man page is provided as a convenience for those users who prefer man pages, although this format is not officially supported by Trolltech.

If you find errors in this manual page, please report them to qt-bugs@trolltech.com. Please include the name of the manual page (qtextstream.3qt) and the Qt version (3.3.8).