The Q3TextStream class provides basic functions for reading and writing text using a QIODevice . 更多...
| 头: | #include <Q3TextStream> |
注意: 此类的所有函数 可重入 .
| enum | 编码 { Locale, Latin1, Unicode, UnicodeNetworkOrder, ..., UnicodeUTF8 } |
| Q3TextStream () | |
| Q3TextStream (QIODevice * iod ) | |
| Q3TextStream (QString * str , int filemode ) | |
| Q3TextStream (QByteArray & a , int mode ) | |
| Q3TextStream (FILE * fh , int mode ) | |
| virtual | ~Q3TextStream () |
| bool | atEnd () const |
| QTextCodec * | codec () |
| QIODevice * | device () const |
| int | fill () const |
| int | fill (int f ) |
| int | flags () const |
| int | flags (int f ) |
| int | precision () const |
| int | precision (int p ) |
| QString | read () |
| QString | readLine () |
| Q3TextStream & | readRawBytes (char * s , uint len ) |
| void | reset () |
| void | setCodec (QTextCodec * codec ) |
| void | setDevice (QIODevice * iod ) |
| void | setEncoding (Encoding e ) |
| int | setf (int bits ) |
| int | setf (int bits , int mask ) |
| void | skipWhiteSpace () |
| void | unsetDevice () |
| int | unsetf (int bits ) |
| int | width () const |
| int | width (int w ) |
| Q3TextStream & | writeRawBytes (const char * s , uint len ) |
| Q3TextStream & | operator<< (QChar c ) |
| Q3TextStream & | operator<< (char c ) |
| Q3TextStream & | operator<< (signed short i ) |
| Q3TextStream & | operator<< (unsigned short i ) |
| Q3TextStream & | operator<< (signed int i ) |
| Q3TextStream & | operator<< (unsigned int i ) |
| Q3TextStream & | operator<< (signed long i ) |
| Q3TextStream & | operator<< (unsigned long i ) |
| Q3TextStream & | operator<< (float f ) |
| Q3TextStream & | operator<< (double f ) |
| Q3TextStream & | operator<< (const char * s ) |
| Q3TextStream & | operator<< (const Q3CString & s ) |
| Q3TextStream & | operator<< (const QString & s ) |
| Q3TextStream & | operator<< (void * ptr ) |
| Q3TextStream & | operator>> (QChar & c ) |
| Q3TextStream & | operator>> (char & c ) |
| Q3TextStream & | operator>> (signed short & i ) |
| Q3TextStream & | operator>> (unsigned short & i ) |
| Q3TextStream & | operator>> (signed int & i ) |
| Q3TextStream & | operator>> (unsigned int & i ) |
| Q3TextStream & | operator>> (signed long & i ) |
| Q3TextStream & | operator>> (unsigned long & i ) |
| Q3TextStream & | operator>> (float & f ) |
| Q3TextStream & | operator>> (double & f ) |
| Q3TextStream & | operator>> (char * s ) |
| Q3TextStream & | operator>> (QString & str ) |
| Q3TextStream & | operator>> (Q3CString & str ) |
The Q3TextStream 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:
| 函数 | 含义 |
|---|---|
| bin | 设置 Q3TextStream to read/write binary numbers |
| oct | 设置 Q3TextStream to read/write octal numbers |
| dec | 设置 Q3TextStream to read/write decimal numbers |
| hex | 设置 Q3TextStream to read/write hexadecimal numbers |
| endl | forces a line break |
| flush | 强制 QIODevice to flush any buffered data |
| ws | eats any available whitespace (on input) |
| reset | resets the Q3TextStream to its default mode (see reset ()) |
| qSetW(int) | 设置 field width to the given argument |
| qSetFill(int) | 设置 fill character to the given argument |
| qSetPrecision(int) | 设置 precision to the given argument |
警告: 默认情况下 Q3TextStream 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 Q3TextStream 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 Q3TextStream 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 () 或 setf (). The stream also supports width (), precision () 和 fill ();使用 reset () to reset the defaults.
另请参阅 QDataStream .
| 常量 | 值 |
|---|---|
Q3TextStream::Locale
|
0
|
Q3TextStream::Latin1
|
1
|
Q3TextStream::Unicode
|
2
|
Q3TextStream::UnicodeNetworkOrder
|
3
|
Q3TextStream::UnicodeReverse
|
4
|
Q3TextStream::RawUnicode
|
5
|
Q3TextStream::UnicodeUTF8
|
6
|
见 setEncoding () for an explanation of the encodings.
Constructs a data stream that has no IO device.
Constructs a text stream that uses the IO device iod .
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 () 或 setCodec (), this setting is ignored for text streams that operate on QString .
范例:
QString str; Q3TextStream 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"; Q3TextStream ts( &str, IO_WriteOnly ); ts << "2+2 = " << 2+2; // str == "2+2 = 414"
Note that because QString is Unicode, you should not use readRawBytes () 或 writeRawBytes () on such a stream.
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 ().
范例:
QByteArray array; Q3TextStream 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 ); Q3TextStream ts( &buf ); ts << "pi = " << 3.14 << '\0'; // array == "pi = 3.14" buf.close();
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
Q3TextStream
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.
[虚拟]
Q3TextStream::
~Q3TextStream
()
Destroys the text stream.
The destructor does not affect the current IO device.
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.
该函数在 Qt 4.2 引入。
另请参阅 QIODevice::atEnd ().
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.
该函数在 Qt 4.2 引入。
另请参阅 setCodec ().
Returns the IO device currently set.
该函数在 Qt 4.2 引入。
另请参阅 setDevice () 和 unsetDevice ().
Returns the fill character. The default value is ' ' (space).
该函数在 Qt 4.2 引入。
这是重载函数。
Sets the fill character to f . Returns the previous fill character.
Returns the current stream flags. The default value is 0.
| Flag | 含义 |
|---|---|
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
|
输出 and input only in binary |
oct
|
输出 and input only in octal |
dec
|
输出 and input only in decimal |
hex
|
输出 and input only in hexadecimal |
showbase
|
Annotates numeric outputs with 0b, 0, or 0x if in
bin
,
oct
,或
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
,或
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.
该函数在 Qt 4.2 引入。
这是重载函数。
Sets the stream flags to f . Returns the previous stream flags.
另请参阅 setf (), unsetf (),和 flags ().
Returns the precision. The default value is 6.
该函数在 Qt 4.2 引入。
这是重载函数。
Sets the precision to p . Returns the previous precision setting.
Reads the entire stream from the current position, and returns a string containing the text.
该函数在 Qt 4.2 引入。
另请参阅 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.
该函数在 Qt 4.2 引入。
另请参阅 QIODevice::readLine ().
读取 len bytes from the stream into s 并返回流引用。
The buffer s must be preallocated.
Note that no encoding is done by this function.
警告: The behavior of this function is undefined unless the stream's encoding is set to Unicode or Latin1.
该函数在 Qt 4.2 引入。
另请参阅 QIODevice::readBlock ().
Resets the text stream.
该函数在 Qt 4.2 引入。
另请参阅 setf (), width (), fill (),和 precision ().
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.
该函数在 Qt 4.2 引入。
另请参阅 setEncoding () 和 codec ().
Sets the IO device to iod .
该函数在 Qt 4.2 引入。
另请参阅 device () 和 unsetDevice ().
Sets the encoding of this stream to e ,其中 e is one of the following values:
| 编码 | 含义 |
|---|---|
| 区域设置 | 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 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 | 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. |
区域设置
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.
该函数在 Qt 4.2 引入。
另请参阅 setCodec ().
Sets the stream flag bits bits . Returns the previous stream flags.
相当于
flags( flags() | bits )
.
该函数在 Qt 4.2 引入。
另请参阅 unsetf ().
这是重载函数。
Sets the stream flag bits bits with a bit mask mask . Returns the previous stream flags.
相当于
flags( (flags() & ~mask) | (bits & mask) )
.
Positions the read pointer at the first non-whitespace character.
该函数在 Qt 4.2 引入。
Unsets the IO device. Equivalent to setDevice ( 0 ).
该函数在 Qt 4.2 引入。
另请参阅 device () 和 setDevice ().
Clears the stream flag bits bits . Returns the previous stream flags.
相当于
flags( flags() & ~mask )
.
该函数在 Qt 4.2 引入。
另请参阅 setf ().
Returns the field width. The default value is 0.
该函数在 Qt 4.2 引入。
这是重载函数。
Sets the field width to w . Returns the previous field width.
写入 len 字节来自 s to the stream and returns a reference to the stream.
Note that no encoding is done by this function.
该函数在 Qt 4.2 引入。
另请参阅 QIODevice::writeBlock ().
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 Q3TextStream .
该函数在 Qt 4.2 引入。
这是重载函数。
Writes character c to the stream and returns a reference to the stream.
该函数在 Qt 4.2 引入。
这是重载函数。
写入
short
integer
i
to the stream and returns a reference to the stream.
该函数在 Qt 4.2 引入。
这是重载函数。
Writes an
unsigned
short
integer
i
to the stream and returns a reference to the stream.
该函数在 Qt 4.2 引入。
这是重载函数。
Writes an
int
i
to the stream and returns a reference to the stream.
该函数在 Qt 4.2 引入。
这是重载函数。
Writes an
unsigned
int
i
to the stream and returns a reference to the stream.
该函数在 Qt 4.2 引入。
这是重载函数。
写入
long
int
i
to the stream and returns a reference to the stream.
该函数在 Qt 4.2 引入。
这是重载函数。
Writes an
unsigned
long
int
i
to the stream and returns a reference to the stream.
该函数在 Qt 4.2 引入。
这是重载函数。
写入
float
f
to the stream and returns a reference to the stream.
该函数在 Qt 4.2 引入。
这是重载函数。
写入
double
f
to the stream and returns a reference to the stream.
该函数在 Qt 4.2 引入。
这是重载函数。
Writes a string to the stream and returns a reference to the stream.
字符串 s is assumed to be Latin1 encoded independent of the Encoding set for the Q3TextStream .
该函数在 Qt 4.2 引入。
这是重载函数。
写入 s to the stream and returns a reference to the stream.
字符串 s is assumed to be Latin1 encoded independent of the Encoding set for the Q3TextStream .
该函数在 Qt 4.2 引入。
这是重载函数。
写入 s to the stream and returns a reference to the stream.
该函数在 Qt 4.2 引入。
这是重载函数。
Writes a pointer to the stream and returns a reference to the stream.
The ptr is output as an unsigned long hexadecimal integer.
该函数在 Qt 4.2 引入。
Reads a char c from the stream and returns a reference to the stream. Note that whitespace is not 跳过。
这是重载函数。
Reads a char c from the stream and returns a reference to the stream. Note that whitespace is skipped.
这是重载函数。
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.
这是重载函数。
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.
这是重载函数。
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.
这是重载函数。
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.
这是重载函数。
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.
这是重载函数。
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.
这是重载函数。
读取
float
f
from the stream and returns a reference to the stream. See
flags
() for an explanation of the expected input format.
这是重载函数。
读取
double
f
from the stream and returns a reference to the stream. See
flags
() for an explanation of the expected input format.
这是重载函数。
Reads a "word" from the stream into s 并返回流引用。
A word consists of characters for which isspace() returns FALSE.
这是重载函数。
Reads a "word" from the stream into str 并返回流引用。
A word consists of characters for which isspace() returns FALSE.
这是重载函数。
Reads a "word" from the stream into str 并返回流引用。
A word consists of characters for which isspace() returns FALSE.