Binary files are relatively small, but not readable. A text file is a readable file. to operate such a file, we need to use the QTextStream class. QTextStream is similar to QDataStream, But it operates plain text files. Some text formats, such as XML and HTML, can be generated by QTextStream, but Qt provides a more convenient XML operation class, which is not included here.
QTextStream automatically converts Unicode encoding to the same operating system encoding. This operation is transparent to programmers. It also converts line breaks and does not need to be processed by yourself. QTextStream uses a 16-bit QChar as the basic data storage unit. It also supports C ++ standard types, such as int. In fact, this is to convert the standard type and string.
QTextStream is basically the same as QDataStream. For example, the following code writes "Thomas M. Disch: 334/n" to the tmp.txt file:
- QFile file("sf-book.txt");
- if (!file.open(QIODevice::WriteOnly)) {
- std::cerr << "Cannot open file for writing: "
- << qPrintable(file.errorString()) << std::endl;
- return;
- }
-
- QTextStream out(&file);
- out << "Thomas M. Disch: " << 334 << endl;
As you can see, this code is basically the same as the preceding QDataStream code. It is easy to write text files, but reading is not that easy. For example,
- out << "Denmark" << "Norway";
Is the code we write. We write two words respectively and try to read them in the same format as the binary file:
- in >> str1 >> str2;
The out and in of the above two codes are of the QTextStream type. Although we can write data normally, when reading data, str1 will be DenmarkNorway, and str2 will be empty. Writing data in the form of text cannot differentiate the data truncation location. Because when using QDataStream to write data, it is actually necessary to write such length information before the string. Therefore, a text file is more of a global operation. For example, you can use QTextStream: readLine () to read a row and use QTextStream: readAll () to read all the text, then process the obtained QString object.
By default, QTextStream reads and writes data using the local encoding of the operating system. However, you can use the setCodec () function for settings, such
- stream.setCodec("UTF-8");
Like <iostream>, QTextStream also provides some descriptors for formatting output, called stream manipulators. These descriptors are placed before the output content, or the corresponding functions are used to format the subsequent output content. The specific descriptor is as follows:
SetIntegerBase (int) |
0 |
The number prefix is automatically checked when reading data. |
2 |
Binary |
8 |
Octal |
10 |
Decimal |
16 |
Hexadecimal |
SetNumberFlags (NumberFlags) |
ShowBase |
Display prefix, binary display 0b, octal display 0, hexadecimal display 0x |
ForceSign |
Display symbols before real numbers |
ForcePoint |
Display the dot Separator in a number |
UppercaseBase |
Use an uppercase prefix, such as 0B, 0X |
UppercaseDigits |
Use uppercase letters as hexadecimal digits |
SetRealNumberNotation (RealNumberNotation) |
FixedNotation |
Fixed Point count, for example, 0.000123 |
ScientificNotation |
Scientific notation, such as 1.23e-4 |
SmartNotation |
Indicates a fixed point or scientific notation. A Concise notation is automatically selected. |
SetRealNumberPrecision (int) |
Sets the maximum number of decimal places generated. The default value is 6. |
SetFieldWidth (int) |
Sets the minimum value of a field. The default value is 0. |
SetFieldAlignment (FieldAlignment) |
AlignLeft |
Left aligned |
AlignRight |
Right alignment |
AlignCenter |
Center alignment |
AlignAccountingStyle |
Alignment between symbols and numbers |
SetPadChar (QChar) |
Specifies the character to be filled when alignment is set. The default value is space. |
For example, the following code
- out << showbase << uppercasedigits << hex << 12345678;
0xBC614E is output. Or we can write it like this:
- out.setNumberFlags(QTextStream::ShowBase | QTextStream::UppercaseDigits);
- out.setIntegerBase(16);
- out << 12345678;
QTextStream can be output not only to QIODevice, but also to QString. For example:
- QString str;
- QTextStream(&str) << oct << 31 << " " << dec << 25 << endl;
This article is from the "bean space" blog, please be sure to keep this source http://devbean.blog.51cto.com/448512/297145