Qt Creator module learning-files, directories and input/output (2)

Qt Creator module learning-files, directories and input/output (2)

This article is the focus of reading and writing files. I have used three ways to read and write files. If there are any shortcomings, I hope I can get your suggestions.

If you are a beginner, you can see:
Qt Creator module learning-files, directories and input/output (1)

2. Read and write files (using the basic QFile method)

For the convenience of description in this chapter, I will only write code, which is a small program.
The approximate shape is:
Insert picture description here

Realize the basic opening and saving files.

The slot function can be transferred to the slot using the designer interface,

//打开文件
void MainWindowoid MainWindow::on_pushButton_clicked()
{
    
    
    QString filename = QFileDialog::getOpenFileName(this,
                                                "open","../","TXT(*.txt)");
    if(filename.isEmpty() == false){
    
    
        QFile file(filename);
        if(file.open(QIODevice::ReadOnly)==true){
    
    
            //全部读完
            //读文件,默认只识别utf8,如果想弄别的用文本流
  /*          QByteArray array = file.readAll();
            //显示到编辑区
            //ui->textEdit->setText(QString(array));
            ui->textEdit->setText(array); */
            //一行一行读
            QByteArray array;
            while(file.atEnd()==false){
    
    
                //读一行
               array = file.readLine();
            }
            ui->textEdit->setText(array);
        }
        file.close();
    }
}
//保存文件
void MainWindow::on_pushButton_2_clicked()
{
    
    
   QString filename = QFileDialog::getSaveFileName(this,"save","../","TXT(*.txt)");


   if(filename.isEmpty()==false){
    
    
       QFile file;//创建文件对象
       //关联文件名字
       file.setFileName(filename);
       //打开文件,只写方式
       if(file.open(QIODevice::WriteOnly)==true){
    
    
           //获取内容
           QString str = ui->textEdit->toPlainText();
           //写文件
           //把QString 转化为 QByteArray
           file.write(str.toUtf8());
       }
   }
}

3. Read and write files (using QDataStream to read and write)

This method is stored in binary, we usually call it data stream storage

#include "widget.h"
#include "ui_widget.h"
#include <QDataStream>
#include <QFile>
#include <QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    
    
    ui->setupUi(this);
    writeData();
    readData();
}

Widget::~Widget()
{
    
    
    delete ui;
}
void Widget::writeData()
{
    
    
  QFile file("../test.txt");
  //打开文件,只写方式打开
  if(file.open(QIODevice::WriteOnly)==true){
    
    
      //创建数据流
      //往数据流中输入数据
      QDataStream stream(&file);
      stream << QString("求点赞求关注")<<521;
      file.close();
  }
}

void Widget::readData()
{
    
    
    QFile file("../test.txt");
    //打开文件,只写方式打开
    if(file.open(QIODevice::ReadOnly)==true){
    
    
        //创建数据流
        //往数据流中读取数据
         QDataStream stream(&file);
         //读的时候,按写的顺序取数据
         QString str;
         int a;
         stream >> str >> a;
         qDebug()<<str.toUtf8().data()<<a;
         file.close();
    }
}

Operation result:
uInsert picture description here

4. Read and write files (using QTextStream to read and write)

This method is stored in the form of a text stream

#include "widget.h"
#include "ui_widget.h"
#include <QTextStream>
#include <QFile>
#include <QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    
    
    ui->setupUi(this);
    writeData();
    readData();
}

Widget::~Widget()
{
    
    
    delete ui;
}

void Widget::writeData()
{
    
    
    QFile file;
  //此处的方式都可以,这里用不同的方式是为了让自己知道可以有好几种方式.
  file.setFileName("../demo.txt");
  //打开文件,只写方式打开
  if(true==file.open(QIODevice::WriteOnly))
  {
    
    
      QTextStream stream(&file);
      //指定编码,
      stream.setCodec("UTF-8");
      stream<<QString("求关注")<<521;
      file.close();
  }
}

void Widget::readData()
{
    
    
    QFile file("../demo.txt");
    //打开文件,只写方式打开
    if(true==file.open(QIODevice::ReadOnly)){
    
    
        QTextStream stream(&file);
        //指定编码,
        stream.setCodec("UTF-8");
        QString str;
        int a;
        stream >> str >> a;
        qDebug()<<str.toUtf8().data()<<a;
        file.close();
    }
}

Operation result: In
Insert picture description here
this way, you can see that there is a 0 at the end of my output. This output itself is problematic, but it is not a big problem when using the interface design.

QString str = stream.readAll();
ui->textEdit->setText(str);

There will be no problem with this.

5.buffer (memory file)

   QByteArray array;
    QBuffer memFile;
    memFile.open(QIODevice::WriteOnly);
    //加/n是换行
    memFile.write("111\n");
    memFile.write("222");
    memFile.close();


    qDebug()<<memFile.buffer();
    qDebug()<<"array="<<array;


    QBuffer memFile1;
    memFile1.open(QIODevice::WriteOnly);
    QDataStream stream(&memFile1);
    stream<<QString("测试")<<521;
    memFile1.close();
     
    // 该方式读不出来
    qDebug()<<QString(memFile1.buffer()).toUtf8().data();
    
    memFile1.open(QIODevice::ReadOnly);
    QDataStream stream1;
    stream1.setDevice(&memFile1);
    QString str;int a;
    stream1>>str>>a;
    memFile1.close();
    qDebug()<<str.toUtf8().data()<<a;

Results of operation:
Insert picture description hereAmong the four methods, the one I use most as a student should be the first one. You can take a good look at the second and third methods. Personally, I feel very useful.

Guess you like

Origin blog.csdn.net/m0_50210478/article/details/108148463