In the previous chapter we described the reading and writing of binary files. Binary files are small, but not human-readable formats. A text file is a human-readable file. In order to manipulate this file, we need to use QTextStream
classes. QTextStream
QDataStream
is similar to the usage, except that it operates on plain text files. In addition, such as XML, HTML, although it is also a text file, can be QTextStream
generated, but Qt provides a more convenient XML operation class, this does not include this part of the content.
QTextStream
Unicode encoding is automatically converted to the encoding of the operating system, which is transparent to the developer. It also converts line breaks and does not need to be handled by itself. QTextStream
using a 16-bit QChar
base data storage unit, it also supports C + + standard types, such as int. In fact, this is the conversion of this standard type to a string.
QTextStream
The same QDataStream
use, for example, the following code will write "The answer is 42" to the File.txt file:
12345 |
QFile Data("file.txt"); if (data. Open(QFile::WriteOnly | qiodevice::Truncate)) { Qtextstream out (&data); Out << "The answer" << ; } |
Here, we open()
add the open mode in the function QIODevice::Truncate
. We can see the difference in these opening methods from the following table:
Enumeration values |
Describe |
QIODevice::NotOpen |
Not open |
QIODevice::ReadOnly |
Open as read-only |
QIODevice::WriteOnly |
Open in write-only mode |
QIODevice::ReadWrite |
Open in read and write mode |
QIODevice::Append |
Open in Append mode, new additions will be appended to the end of the file |
QIODevice::Truncate |
Opened in an overridden manner, the original data is purged when the new data is written, and the cursor is set at the beginning of the file. |
QIODevice::Text |
When reading, converts the line terminator to \ n, and when writing, converts the line terminator to a cost-based format, such as \ r \ n on the Win32 platform |
QIODevice::Unbuffered |
Ignore cache |
We used it here QFile::WriteOnly | QIODevice::Truncate
to manipulate files in the form of write-only and overwrite existing content. Note that the QIODevice::Truncate
contents of the file are emptied directly.
While QTextStream
the write content is QDataStream
consistent, it can be difficult to read:
1234567 |
QFile Data("file.txt"); if (data. Open(QFile::ReadOnly)) { Qtextstream in(&data); QString str; int ans = 0; In >> str >> ans; } |
In use QDataStream
, such code is very convenient, but the use of the QTextStream
time is different: when read out, STR will be the answer is 42,ans is 0. This is because the data is written as text, and there is no separation between the data. Remember what we said earlier that QDataStream
when you use write, you actually add an extra length value to the content before the content you want to write. The text file does not have a similar operation. As a result, when using a text file, it is seldom broken up to read, instead it is used such as QTextStream::readLine()
reading a line, using the QTextStream::readAll()
function of reading all the text, and then processing the obtained QString
object.
By default, QTextStream
the encoding format is Unicode, and if we need to use a different encoding, you can use the
1 |
Stream. Setcodec("UTF-8"); |
Such a function is set.
Also, for convenience, QTextStream
std::cout
Many descriptors are provided, called Stream manipulators. Because the text file is for people to read, naturally need a good format (compared to the binary file does not have these problems, as long as the data is accurate). These descriptors are shorthand for some functions that we can find in the documentation:
Descriptor |
Equivalent to |
bin |
setIntegerBase(2) |
oct |
setIntegerBase(8) |
dec |
setIntegerBase(10) |
hex |
setIntegerBase(16) |
showbase |
setNumberFlags(numberFlags() | ShowBase) |
forcesign |
setNumberFlags(numberFlags() | ForceSign) |
forcepoint |
setNumberFlags(numberFlags() | ForcePoint) |
noshowbase |
setNumberFlags(numberFlags() & ~ShowBase) |
noforcesign |
setNumberFlags(numberFlags() & ~ForceSign) |
noforcepoint |
setNumberFlags(numberFlags() & ~ForcePoint) |
uppercasebase |
setNumberFlags(numberFlags() | UppercaseBase) |
uppercasedigits |
setNumberFlags(numberFlags() | UppercaseDigits) |
lowercasebase |
setNumberFlags(numberFlags() & ~UppercaseBase) |
lowercasedigits |
setNumberFlags(numberFlags() & ~UppercaseDigits) |
fixed |
setRealNumberNotation(FixedNotation) |
scientific |
setRealNumberNotation(ScientificNotation) |
left |
setFieldAlignment(AlignLeft) |
right |
setFieldAlignment(AlignRight) |
center |
setFieldAlignment(AlignCenter) |
endl |
operator<<(‘\n‘) Andflush() |
flush |
flush() |
reset |
reset() |
ws |
skipWhiteSpace() |
bom |
setGenerateByteOrderMark(true) |
These descriptors are just shorthand for some functions. For example, if we want to output a binary form of 12345678, you can use the
1 |
Out << bin << 12345678; |
You can do it. This is equivalent to
12 |
out. Setintegerbase(2); out << 12345678; |
More complicated if we want to be comfortable with a 1234567890 hexadecimal format with a prefix and all uppercase letters (0XBC614E), then just use the
1 |
Out << showbase << uppercasedigits << hex << 12345678; |
Can.
Not only is it QIODevice
QTextStream
possible to output the content directly QString
. For example
12 |
QString str; Qtextstream (&str << oct << 31 << << dec << 25 << endl |
This provides a simple way to handle the contents of a string.
Qt Learning Path: text file reading and writing