Qt learning path (57): text file read/write

Source: Internet
Author: User

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:

 
 
  1. QFile file("sf-book.txt");  
  2. if (!file.open(QIODevice::WriteOnly)) {  
  3.     std::cerr << "Cannot open file for writing: " 
  4.               << qPrintable(file.errorString()) << std::endl;  
  5.     return;  
  6. }  
  7.  
  8. QTextStream out(&file);  
  9. 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,

 
 
  1. 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:

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

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

 
 
  1. out << showbase << uppercasedigits << hex << 12345678; 

0xBC614E is output. Or we can write it like this:

 
 
  1. out.setNumberFlags(QTextStream::ShowBase | QTextStream::UppercaseDigits);  
  2. out.setIntegerBase(16);  
  3. out << 12345678; 

QTextStream can be output not only to QIODevice, but also to QString. For example:

 
 
  1. QString str;  
  2. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.