1

I am trying to read in a text file and then parse each line into 3 variables. Two of them are QStrings, and one of them is an int. Each line of the file will look like this:

This is my first string : This is my second string : 2
This is another first string : This is another second string : 18

Based on this, as I iterate through my file, I would like to set each variable (QString1, QString2, int) and then call a method.

QString1 = "This is my first string"; 
QString2 = "This is my second string"; 
int x = 2;

callMethod(QString1, QString2, x);

QString1 = "This is another first string"; 
QString2 = "This is another second string"; 
int x = 18;

callMethod(QString1, QString2, x);

How do I do this?

Dimitri Podborski
  • 3,368
  • 3
  • 14
  • 26
user3781214
  • 357
  • 1
  • 3
  • 14
  • It seems like your lines are broken up by a colon (i.e. s : s : 1) is that correct? – Tas Jun 19 '16 at 00:06
  • No not lines, each variable value is. All 3 variables will be on 1 line, separated by " : " – user3781214 Jun 19 '16 at 00:07
  • [look at his answer](http://stackoverflow.com/questions/14265581/parse-split-a-string-in-c-using-string-delimiter-standard-c) which addresses how to split a string on a delimiter character – Roland Jun 19 '16 at 00:12

2 Answers2

4

You can read the file line-by-line and then simply parse the results as they flow through. Here is an example:

#include <QFile>
#include <QTextStream>
#include <QFileDialog>
#include <QDebug>

void MainWindow::parseFile()
{
    QFile inputFile(QFileDialog::getOpenFileName());
    if (inputFile.open(QIODevice::ReadOnly))
    {
       QTextStream in(&inputFile);
       while (!in.atEnd())
       {
           QString text = in.readLine();
          if (!text.isEmpty())
          {
              QStringList line = text.split(':');
              callMethod(line[0], line[1], QString(line[2]).toInt());
          }
       }
       inputFile.close();
    }
}

void MainWindow::callMethod(QString first, QString second, int third)
{
    qDebug() << "Variable #1: " << first;
    qDebug() << "Variable #2: " << second;
    qDebug() << "Variable #3: " << third;
}
Nicholas Johnson
  • 822
  • 1
  • 11
  • 33
1

Here's how you'd do it with proper error checking.

Since it's a text file, you'd set a QTextStream on it, and read it line-by-line. You immediately split the line on the colon separator, and check if you've got three pieces. It is not necessary to check for an empty line, since split can handle empty strings, as well as strings with no separators.

If you got three pieces, parse the last piece to an integer and check if it succeeded. If it did, you can call your method, passing it the first two pieces with whitespace (if any) trimmed, and the integer you parsed.

You most likely want whitespace trimming, so that the behavior for the string parts is the same as for the integer part: toInt ignores whitespace, after all.

Output:

read "a1" "b1" 10
read "Lol" "cats" 33
// https://github.com/KubaO/stackoverflown/tree/master/questions/simple-parse-37902620
#include <QtCore>

void callMethod(const QString & a, const QString & b, int c) {
   qDebug() << "read" << a << b << c;
}

void parse(QIODevice * input)
{
   QTextStream in(input);
   while (!in.atEnd()) {
      auto const line = in.readLine();
      auto const parts = line.split(':');
      if (parts.size() == 3) {
         bool ok;
         auto p3 = parts.at(2).toInt(&ok);
         if (ok)
            callMethod(parts.at(0).trimmed(), parts.at(1).trimmed(), p3);
      }
   }
}

int main(int argc, char ** argv) {
   QCoreApplication app{argc, argv};
   QByteArray data{" a1 :  b1  : 10   \nLol:cats: 33"};
   QBuffer buf(&data);
   if (buf.open(QIODevice::ReadOnly))
      parse(&buf);
}
Kuba hasn't forgotten Monica
  • 88,505
  • 13
  • 129
  • 275