Qt读文本文件

文本文件内容:

5 19total value

6 20 abcde

读法:

   QFile fileIn("test1.txt");

   if(!fileIn.open(QIODevice::ReadOnly))

   {

      QMessageBox::information(this,tr("打开文件失败!"),fileIn.errorString());

      return;

   }

   QTextStream in(&fileIn);

   while(!in.atEnd())

   {

      QString line=in.readLine();

      QStringList fields=line.split(' ');

      if(fields.size()>=3)

      {

         int row=fields.takeFirst().toInt();

         int col=fields.takeFirst().toInt();

         QString val=fields.join(" ");

         qDebug()<<row<<" "<<col<<" "<<val<<endl;

      }

   }

   if(fileIn.isOpen()) fileIn.close();

QString也可以做QIODevice:

   QString strin="5 19 total value\n6 20 abc de";

   QTextStream in(&strin);

   while(!in.atEnd())

   {

      QString line=in.readLine();

      QStringList fields=line.split(' ');

      if(fields.size()>=3)

      {

         int row=fields.takeFirst().toInt();

         int col=fields.takeFirst().toInt();

         QString val=fields.join(" ");

         qDebug()<<row<<" "<<col<<" "<<val<<endl;

      }

   }

 

你可能感兴趣的:(qt)