ISLEman
Public Types | Public Member Functions | Static Public Attributes | List of all members
QTextStream Class Reference

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

#include <qtextstream.h>

Inheritance diagram for QTextStream:
QTextIStream QTextOStream

Public Types

enum  Encoding {
  Locale, Latin1, Unicode, UnicodeNetworkOrder,
  UnicodeReverse, RawUnicode, UnicodeUTF8
}
 
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
}
 

Public Member Functions

void setEncoding (Encoding)
 
void setCodec (QTextCodec *)
 
 QTextStream ()
 
 QTextStream (QIODevice *)
 
 QTextStream (QString *, int mode)
 
 QTextStream (QString &, int mode)
 
 QTextStream (QByteArray, int mode)
 
 QTextStream (FILE *, int mode)
 
virtual ~QTextStream ()
 
QIODevicedevice () const
 
void setDevice (QIODevice *)
 
void unsetDevice ()
 
bool atEnd () const
 
bool eof () const
 
QTextStreamoperator>> (QChar &)
 
QTextStreamoperator>> (char &)
 
QTextStreamoperator>> (signed short &)
 
QTextStreamoperator>> (unsigned short &)
 
QTextStreamoperator>> (signed int &)
 
QTextStreamoperator>> (unsigned int &)
 
QTextStreamoperator>> (signed long &)
 
QTextStreamoperator>> (unsigned long &)
 
QTextStreamoperator>> (float &)
 
QTextStreamoperator>> (double &)
 
QTextStreamoperator>> (char *)
 
QTextStreamoperator>> (QString &)
 
QTextStreamoperator>> (QCString &)
 
QTextStreamoperator<< (QChar)
 
QTextStreamoperator<< (char)
 
QTextStreamoperator<< (signed short)
 
QTextStreamoperator<< (unsigned short)
 
QTextStreamoperator<< (signed int)
 
QTextStreamoperator<< (unsigned int)
 
QTextStreamoperator<< (signed long)
 
QTextStreamoperator<< (unsigned long)
 
QTextStreamoperator<< (float)
 
QTextStreamoperator<< (double)
 
QTextStreamoperator<< (const char *)
 
QTextStreamoperator<< (const QString &)
 
QTextStreamoperator<< (const QCString &)
 
QTextStreamoperator<< (void *)
 
QTextStreamreadRawBytes (char *, uint len)
 
QTextStreamwriteRawBytes (const char *, 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)
 
int fill () const
 
int fill (int)
 
int precision () const
 
int precision (int)
 

Static Public Attributes

static const int basefield = I_BASE_MASK
 
static const int adjustfield
 
static const int floatfield
 

Detailed Description

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

endl bin oct dec hex flush ws

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

Constructor & Destructor Documentation

§ QTextStream() [1/6]

QTextStream::QTextStream ( )

Constructs a data stream that has no IO device.

§ QTextStream() [2/6]

QTextStream::QTextStream ( QIODevice iod)

Constructs a text stream that uses the IO device iod.

§ QTextStream() [3/6]

QTextStream::QTextStream ( QString str,
int  filemode 
)

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

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 since QString is Unicode, you should not use readRawBytes() or writeRawBytes() on such a stream.

§ QTextStream() [4/6]

QTextStream::QTextStream ( QString str,
int  filemode 
)

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

§ QTextStream() [5/6]

QTextStream::QTextStream ( QByteArray  a,
int  mode 
)

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() [6/6]

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

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

Destructs the text stream.

The destructor does not affect the current IO device.

Member Function Documentation

§ atEnd()

bool QTextStream::atEnd ( ) const
inline

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

§ device()

QIODevice * QTextStream::device ( ) const
inline

Returns the IO device currently set.

See also
setDevice(), unsetDevice()

§ eof()

bool QTextStream::eof ( ) const
inline

This function has been renamed to atEnd().

See also
QIODevice::atEnd()

§ fill() [1/2]

int QTextStream::fill ( ) const
inline

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

§ fill() [2/2]

int QTextStream::fill ( int  f)
inline

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

§ flags() [1/2]

int QTextStream::flags ( ) const
inline

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

§ flags() [2/2]

int QTextStream::flags ( int  f)
inline

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

See also
setf(), unsetf(), flags()

§ operator<<() [1/14]

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

Writes a 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.

§ operator<<() [2/14]

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

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

§ operator<<() [3/14]

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

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

§ operator<<() [4/14]

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

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

§ operator<<() [5/14]

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

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

§ operator<<() [6/14]

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

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

§ operator<<() [7/14]

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

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

§ operator<<() [8/14]

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

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

§ operator<<() [9/14]

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

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

§ operator<<() [10/14]

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

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

§ operator<<() [11/14]

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

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.

§ operator<<() [12/14]

QTextStream & QTextStream::operator<< ( const QString s)

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

§ operator<<() [13/14]

QTextStream & QTextStream::operator<< ( const QCString s)

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.

§ operator<<() [14/14]

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.

§ operator>>() [1/13]

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

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

§ operator>>() [2/13]

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

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

§ operator>>() [3/13]

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.

§ operator>>() [4/13]

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.

§ operator>>() [5/13]

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.

§ operator>>() [6/13]

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.

§ operator>>() [7/13]

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.

§ operator>>() [8/13]

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.

§ operator>>() [9/13]

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.

§ operator>>() [10/13]

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.

§ operator>>() [11/13]

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

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

§ operator>>() [12/13]

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

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

§ operator>>() [13/13]

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

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

§ precision() [1/2]

int QTextStream::precision ( ) const
inline

Returns the precision. The default value is 6.

§ precision() [2/2]

int QTextStream::precision ( int  p)
inline

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

§ read()

QString QTextStream::read ( )

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

See also
QIODevice::readLine()

§ readLine()

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

§ readRawBytes()

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.

Note that 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()

§ reset()

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(), precision()

§ setCodec()

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

§ setDevice()

void QTextStream::setDevice ( QIODevice iod)

Sets the IO device to iod.

See also
device(), unsetDevice()

§ setEncoding()

void QTextStream::setEncoding ( Encoding  e)

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

  • Locale Using local file format (Latin1 if locale is not set), but autodetecting Unicode(utf16) on input.
  • Unicode Using 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 QString).
  • 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 Using network order Unicode(utf16) for input and output. Useful when reading Unicode data that does not start with the byte order marker.
  • UnicodeReverse Using reverse network order Unicode(utf16) for input and output. Useful when reading Unicode data that does not start with the byte order marker, or writing data that should be read by buggy Windows applications.
  • RawUnicode Like Unicode, but does not write the byte order marker, nor does it autodetect the byte order. Only useful 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 a 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/written from the stream.

See also
setCodec()

§ setf() [1/2]

int QTextStream::setf ( int  bits)
inline

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

Equivalent to flags( flags() | bits ).

See also
setf(), unsetf()

§ setf() [2/2]

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

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

§ skipWhiteSpace()

void QTextStream::skipWhiteSpace ( )

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

§ unsetDevice()

void QTextStream::unsetDevice ( )

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

See also
device(), setDevice()

§ unsetf()

int QTextStream::unsetf ( int  bits)
inline

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

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

See also
setf()

§ width() [1/2]

int QTextStream::width ( ) const
inline

Returns the field width. The default value is 0.

§ width() [2/2]

int QTextStream::width ( int  w)
inline

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

§ writeRawBytes()

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

Member Data Documentation

§ adjustfield

const int QTextStream::adjustfield
static
Initial value:
= ( QTextStream::left |
QTextStream::right |
QTextStream::internal )

§ floatfield

const int QTextStream::floatfield
static
Initial value:
= ( QTextStream::scientific |
QTextStream::fixed )

The documentation for this class was generated from the following files: