QT中读取文本数据(txt)

      下面的代码实现读取txt文档中的数据,并且是一行一行的读取。

void MainWindow::on_pushButton_clicked()
{
  QFile file("abcd.txt");
  if(! file.open(QIODevice::ReadOnly|QIODevice::Text))
      qDebug()<<file.errorString();
  else
       qDebug()<<"openok";
  file.seek(0);

  QTextStream shuru(&file);
  while(! shuru.atEnd())
  {
      QString line=shuru.readLine();
      qDebug()<<line;
  }
  file.close();
}

QT中读取文本数据(txt)_第1张图片

QT中读取文本数据(txt)_第2张图片

 

上面输出的是字符串格式,可能有时候要用到的是int float这样的格式,程序修改如下:

void MainWindow::on_pushButton_clicked()
{
  int aa;
  int bb;
  int cc;
  char ch;
  float dd;

  QFile file("abcd.txt");
  if(! file.open(QIODevice::ReadOnly|QIODevice::Text))
      qDebug()<

 QT中读取文本数据(txt)_第3张图片

转载于:https://www.cnblogs.com/yibanshouxi/p/4022071.html

你可能感兴趣的:(QT中读取文本数据(txt))