当前位置:网站首页>Day12QFile2021-09-27

Day12QFile2021-09-27

2022-06-22 02:44:00 Morning and evening rain

QFile—— File operations

Qt The file operation of is very simple ,Qt Provided in the QIODevice Class as all I/O Equipment foundation interface , by IO Equipment such as QFile Provides an abstract interface .QFile Is a readable and writable text file 、 Binary 、 as well as qt Of resource files IO equipment . Before accessing a device , Need to use open() Function to open the device , And the correct opening mode must be established . Use write()、putchar Method write ,read(),readline()、readAll() Method read , with QTextStream or QDataStream Use a combination of , More convenient . So after the operation ends , You should use close() Method to close the file , Keep good habits .

Case study :

Output txt file , Separate use write and QTextStream There are two ways to write .
1. stay .cpp or .h Add header file to

#include <QFile>
#include <QTextStream>

2.cpp The code in the file is as follows , First, construct the file object , Create a file by parameter name , And then to append Way to open a file , Use QTextStream Class to write “ written words ”, Use write() Function writes a string , Last call close() Close file .

 	QFile file("Day12.txt");
    file.open(QIODevice::Append);
    // Use QTextStream write file 
    QTextStream tsm(&file);
    QString str = " Please believe that , All accumulation has its specific value !\r\n";
    tsm<<str<<"Designed by Jack chen";
    file.write("Day12\r\n");
    file.close();

3. Running effect : Generated under the build directory Day12.txt file , The contents are as follows :
 Insert picture description here

原网站

版权声明
本文为[Morning and evening rain]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211658130485.html