I need help grabbing some integers from a Qt QString. I have a file, and I'm storing different lines, similar to this in it:
Fitness: 4 123456789123456789123456789
Fitness: 3 135791357913579135791357913
....and so on. First, I'm trying to find the one with the highest fitness (up above, on the line 'Fitness: 4 .....' 4 is the fitness level), and the one with the second highest fitness. Then I'm going through the ones with the highest and second highest fitness, and copying the 27 numbers after the fitness level, into a 2D array 'readIn'. Those are the parts I'm stuck on. Here's my code:
void MainWindow::SpliceGenes(){
int secHighest = 0;
int Highest = 0;
int howFar = 0;
QFile file("C:/Users/Quentin/Documents/NeuralNetInfo.txt");
if(file.open(QIODevice::ReadWrite)){
QTextStream stream(&file);
QString text = stream.readAll();
while(text.indexOf(": ", howFar) != -1){//this should make sure it goes through the entire file until it's gotten all of the fitness values.
if(QString.toInt(text[text.indexOf(": ", howFar) + 2]) > Highest){//this should be: if the number after the ': ' is higher than the current
//highest fitness value...
secHighest = Highest;
Highest = QString.toInt(text[text.indexOf(": ", howFar) + 1]);//should be: the new highest value equals the number after the ': '
howFar = text.indexOf(": ", howFar) + 5;//I use howFar to skip past ': ' I've already looked at.
//5 is a random number that makes sure it's past the ': ' it was just on } } //the rest doesn't really matter (I don't think) ReadNeuralNet(Highest, secHighest);
for(int i = 0; i< 3; i++){
readIn[(qrand() % 9)] [i] = readInTwo[qrand() % 9] [i];
}
}
}
These are the errors I'm getting:
//on 'if(QString.toInt(text[text.indexOf(": ", howFar) + 2]) > Highest){'
error: C2059: syntax error: '.'
error: C2143: syntax error: missing ';' before '{'
//on 'Highest = QString.toInt(text[text.indexOf(": ", howFar) + 1]);'
error: C2275: 'QString': illegal use of this type as an expression
error: C2228: left of '.toInt' must have class/struct/union
//and on the last curly bracket
error: C1903: unable to recover from previous error(s); stopping compilation
Any help is appreciated. Thanks in advance
The definition of toInt()
is int QString::toInt(bool *ok = Q_NULLPTR, int base = 10) const
which is not static, meaning you need an object to work with. text
is a QString
so you can use its .toInt()
method.
There is a lot wrong in your code. indexOf
returns an int which is the position of the found text, or -1 if not found.
You can use mid
in combination with the index (if found) to cut out the part you want to convert.
Also it would be better to use readLine
instead of readAll
and loop through the QTextStream
processing each line.
Possible implementation:
QFile file { QStringLiteral("NeuralNetInfo.txt") };
if(file.open(QIODevice::ReadOnly))
{
auto pos { 0 };
QString line { QStringLiteral("") };
QTextStream stream { &file };
while(!stream.atEnd())
{
line = stream.readLine();
pos = line.indexOf(QStringLiteral(": "));
if(pos)
{
pos += 2;
if(pos < line.length())
{
qDebug() << line.mid(pos , 1);
}
}
}
}
Output:
"4"
"3"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.