QTextStream(3qt)

๐Ÿ“ฆ version 3.3.8b, in fedora 43, fedora 44

๐Ÿ’– NAME ๐Ÿ’–

Okay so, QTextStream โ€” this is the class that gives you basic functions for reading and writing text using a QIODevice. That's the whole assignment, bestie. โœจ

(This is the 3qt manual, for the record.)


๐ŸŒธ SYNOPSIS ๐ŸŒธ

First thing first, and it's a big one: all the functions in this class are reentrant when Qt is built with thread support. Reentrant. She's safe in threads. Periodt. ๐Ÿ‘‘

#include <qtextstream.h>

Inherited by QTextIStream and QTextOStream. ๐Ÿซถ

๐ŸŽ€ Public Members ๐ŸŽ€

Here's the full cast, no one gets left out:

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. Iconic behaviour honestly.

The text stream class has a functional interface that is very similar to that of the standard C++ iostream class. So if you've met iostream, you two already know each other. ๐Ÿ’…

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

Not me spotting a glitch ๐Ÿ™ˆ โ€” in the original page this spot is a table that did not survive the conversion to a manual page (all that's left behind is a <center>.nf husk). The sentence above is genuinely all the source gives us here. No cap.
โš ๏ธ Warning, and this one actually matters: 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. So yes, your 0100 is not a hundred, bestie, it's sixty-four. ๐Ÿ˜ญ

The QTextStream class reads and writes text; it is not appropriate for dealing with binary data (but QDataStream is). Text girlies only. ๐Ÿ“

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

The whole lineup, in order:

  • 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. She's just vibing, unattached. ๐Ÿฆ‹

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

Heads up: if you set an encoding or codec with setEncoding() or setCodec(), this setting is ignored for text streams that operate on QString. Noted, bestie. โœ๏ธ

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. Just don't. ๐Ÿ™ˆ

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. ๐Ÿ’€ Name your streams carefully, I'm begging.

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:

Another little conversion casualty ๐Ÿ˜ญ โ€” the original just says "Examples:" and then shows nothing. That's it. That's the whole thing.

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.

Okay so the original has a table of flags right here, and it did not survive the trip ๐Ÿฅฒ โ€” all that's left is the word Flag and a divider line. The table's contents are simply not in this page. Moving on!

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. So yeah, that's a whole thing. ๐ŸŒˆ

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 )

okay so this one is an overloaded member function, it's there for convenience, and it behaves basically like the one right above it. bestie behavior fr fr.

It writes an unsigned long int i to the stream and hands you back a reference to the stream. ๐Ÿซถ


โœจ QTextStream & QTextStream::operator<< ( float f )

overloaded member function, provided for convenience, behaves essentially like the above function. same vibe, different type, we love a consistent queen. ๐Ÿ’…

Writes a float f to the stream and returns a reference to the stream. ๐ŸŒธ


๐Ÿฆ‹ QTextStream & QTextStream::operator<< ( double f )

overloaded member function, provided for convenience, behaves essentially like the above function. (yes we're doing this again, there's like eight of them, it's giving overloaded era ๐Ÿ˜ญ)

Writes a double f to the stream and returns a reference to the stream. โญ


๐ŸŽ€ QTextStream & QTextStream::operator<< ( const char * s )

overloaded member function, provided for convenience, behaves essentially like the above function. ok so:

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

โš ๏ธ Important bestie detail: the string s is assumed to be Latin1 encoded independent of the Encoding set for the QTextStream. So no, it does not care what encoding you set. It's Latin1 and it said what it said. ๐Ÿ’€

๐Ÿ’œ QTextStream & QTextStream::operator<< ( const QString & s )

overloaded member function, provided for convenience, behaves essentially like the above function.

Writes s to the stream and returns a reference to the stream. no extra drama, no encoding plot twist. ๐ŸŒˆ


๐Ÿงš QTextStream & QTextStream::operator<< ( const QCString & s )

overloaded member function, provided for convenience, behaves essentially like the above function.

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

โš ๏ธ same deal as the const char * one: s is assumed to be Latin1 encoded independent of the Encoding set for the QTextStream. iconic but stubborn. ๐Ÿ’…

๐Ÿ‘‘ QTextStream & QTextStream::operator<< ( void * ptr )

overloaded member function, provided for convenience, behaves essentially like the above function.

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

ptr gets output as an unsigned long hexadecimal integer. so it's giving hex, lowkey fancy. ๐Ÿ”ฅ


๐ŸŒธ 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. like, at all. it will read the whitespace. no thoughts, no skipping, just vibes.

โœจ QTextStream & QTextStream::operator>> ( char & c )

overloaded member function, provided for convenience, 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 here. (yes this is the opposite of the QChar one above. the way that they differ ๐Ÿ’€)

๐Ÿ“ QTextStream & QTextStream::operator>> ( signed short & i )

overloaded member function, provided for convenience, behaves essentially like the above function.

Reads a signed short integer i from the stream and returns a reference to the stream. peep flags() for an explanation of the expected input format. ๐Ÿ‘€


๐ŸŒˆ QTextStream & QTextStream::operator>> ( unsigned short & i )

overloaded member function, provided for convenience, behaves essentially like the above function.

Reads an unsigned short integer i from the stream and returns a reference to the stream. peep flags() for an explanation of the expected input format. ๐Ÿ‘€


๐Ÿ’– QTextStream & QTextStream::operator>> ( signed int & i )

overloaded member function, provided for convenience, behaves essentially like the above function.

Reads a signed int i from the stream and returns a reference to the stream. peep flags() for an explanation of the expected input format. ๐Ÿ‘€


๐Ÿฆ‹ QTextStream & QTextStream::operator>> ( unsigned int & i )

overloaded member function, provided for convenience, behaves essentially like the above function.

Reads an unsigned int i from the stream and returns a reference to the stream. peep flags() for an explanation of the expected input format. ๐Ÿ‘€


๐Ÿ’œ QTextStream & QTextStream::operator>> ( signed long & i )

overloaded member function, provided for convenience, behaves essentially like the above function.

Reads a signed long int i from the stream and returns a reference to the stream. peep flags() for an explanation of the expected input format. ๐Ÿ‘€


๐ŸŽ€ QTextStream & QTextStream::operator>> ( unsigned long & i )

overloaded member function, provided for convenience, behaves essentially like the above function.

Reads an unsigned long int i from the stream and returns a reference to the stream. peep flags() for an explanation of the expected input format. ๐Ÿ‘€


๐Ÿงš QTextStream & QTextStream::operator>> ( float & f )

overloaded member function, provided for convenience, behaves essentially like the above function.

Reads a float f from the stream and returns a reference to the stream. peep flags() for an explanation of the expected input format. ๐Ÿ‘€


๐Ÿ‘‘ QTextStream & QTextStream::operator>> ( double & f )

overloaded member function, provided for convenience, behaves essentially like the above function.

Reads a double f from the stream and returns a reference to the stream. peep flags() for an explanation of the expected input format. ๐Ÿ‘€


๐Ÿ“ QTextStream & QTextStream::operator>> ( char * s )

overloaded member function, provided for convenience, behaves essentially like the above function.

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

and what IS a word, bestie? a word consists of characters for which isspace() returns FALSE. so basically everything that is NOT whitespace. no cap. ๐Ÿ’…


๐ŸŒธ QTextStream & QTextStream::operator>> ( QString & str )

overloaded member function, provided for convenience, 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. same energy, different container. โœจ


๐Ÿ’– QTextStream & QTextStream::operator>> ( QCString & str )

overloaded member function, provided for convenience, 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. third time's the charm, we ate. ๐Ÿ”ฅ


๐ŸŒˆ int QTextStream::precision () const

Returns the precision. the default value is 6. noted. ๐Ÿ“


๐Ÿฆ‹ int QTextStream::precision ( int p )

overloaded member function, provided for convenience, behaves essentially like the above function.

Sets the precision to p. and it returns the previous precision setting, because it's polite like that. ๐Ÿซถ


๐ŸŽ€ QString QTextStream::read ()

Reads the entire stream from the current position, and returns a string containing the text. the whole thing. all of it. main character energy. ๐Ÿ‘‘

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. ๐Ÿšจ And heads up: this is different from QIODevice::readLine(), which does not strip the newline at the end of the line. so like, know which one you're calling bestie.

On EOF you will get a QString that is null. On reading an empty line the returned QString is empty but not null. yes these are two different things and yes it matters. ๐Ÿ’€

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. you gotta bring your own space, it is not allocating for you. ๐Ÿ™ˆ

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. like, genuinely undefined, not "teehee it might be weird" undefined. set your encoding first, bestie, i am begging. ๐Ÿ˜ญ

See also QIODevice::readBlock().


โœจ void QTextStream::reset ()

Resets the text stream. factory reset but make it cute. ๐ŸŽ€

Everything it does:

  • 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. It will not try to autodetect Unicode. it's not guessing, it's not snooping, it's just doing what you told it. ๐Ÿ’

๐Ÿšจ Note that this function should be called before any data is read to/written from the stream. like, BEFORE. not during. not after. before.

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>

and the vibe here is important: 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. so it peeks, figures out the tea, and removes the marker. ๐Ÿ‘€

๐Ÿšจ Note that this function should be called before any data is read to or written from the stream. (same rule as setCodec(), it's a whole theme.)

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 )

overloaded member function, provided for convenience, 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. it's just scooting past the blank space, no thoughts. ๐Ÿ›ผ


๐ŸŒˆ 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 )

overloaded member function, provided for convenience, behaves essentially like the above function.

Sets the field width to w. Returns the previous field width. so generous, always giving you the old one back. ๐Ÿซถ


๐Ÿ‘‘ 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. raw is raw, bestie.

See also QIODevice::writeBlock().


๐ŸŒŸ SEE ALSO ๐ŸŒŸ


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. (not a human, not a bestie, just a script doing its little thing ๐Ÿค–โœจ)


๐Ÿšจ 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. (fr fr, they mean it ๐Ÿ’–)

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


okay that's the whole page bestie, you made it to the end ๐ŸŽ‰๐Ÿ’… this was generated by Trolltech AS on 2 February 2007 for the QTextStream(3qt) manual page, and no cap, you ate that. go forth and stream responsibly ๐Ÿฆ‹๐Ÿ’–โœจ

๐Ÿ“ฆ where this page comes from