0

I have a text file which looks like this:

VariableA = 10 VariableB = 20 VariableC = "Hello World"

The code works fine, but my trouble is getting the text strings between " ".

QStringList Data;
Data << "VariableA = " << "VariableB = " << "VariableC = ";
QStringList Values;

int VariableA;
int VariableB;
QString VariableC;


foreach(const QString &DataToFind, Data) {
    QRegExp DataExpression(DataToFind);
    int DataStart = DataExpression.indexIn(TextToFind);
    if(DataStart >= 0) {
        int DataLength = DataExpression.matchedLength();
        int ValueSize = 1;
        while(TextToFind.at(DataStart + DataLength + ValueSize) != QChar(' ')) {
            ValueSize++;
        }
        QStringRef DataValue(&TextToFind, DataStart + DataLength, ValueSize);
        Values += DataValue.toString(); 
        DataStart = DataExpression.indexIn(description, DataStart + DataLength);
    } else {
        continue;
    }
}

VariableA = Values[0].toInt();
VariableB = Values[1].toInt();
VariableC = Values[2];

The issue is that the text on VariableC can have spaces and/or " (double quotes) inside it. So the method I've posted above to retrieve the variables from the file is useless. Since it uses " " to reach end of variable in the file.

How can I retrieve the full text inside the double quotes?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Blastcore
  • 360
  • 7
  • 19
  • Is the text file always like that? Are just the values of A, B and X changing? –  Dec 22 '12 at 22:12
  • There can be more stuff, and yes, A, B and X can change values. – Blastcore Dec 22 '12 at 22:36
  • This might be a bit tricky and depends on the structure of your file. Are you free to change it or is it provided by someone else? – Zeks Dec 23 '12 at 11:02
  • Also, are you sure you don't want to use QSettings? – Zeks Dec 23 '12 at 11:02
  • It's provided. But also, there are just 3 "values" to read. Nothing more. I mean "A", "B" and "X" will be always there. But their values can be changed freely. – Blastcore Dec 23 '12 at 11:58

1 Answers1

0
QStringList Data;
Data << "A = " << "B = " << "X = ";

int A;
int B;
QString X;

foreach(const QString &DataToFind, Data) {
    QRegExp DataExpression(DataToFind);
    int DataStart = DataExpression.indexIn(TextToFind);
    if(DataStart >= 0) {
        int DataLength = DataExpression.matchedLength();
        int ValueSize = 1;
        while(TextToFind.at(DataStart + DataLength + ValueSize) != QChar(' ')) {
            ValueSize++;
        }
        QStringRef DataValue(&TextToFind, DataStart + DataLength, ValueSize);
        DataStart = DataExpression.indexIn(description, DataStart + DataLength);
    } else {
        continue;
    }
}

This does the work.

Blastcore
  • 360
  • 7
  • 19