PDA

View Full Version : QT - reading line until ":"



gusiak96
15th June 2017, 18:14
Hi,
Could you tell me how to read line from txt file until sign ":"? I have file like this:

mathematics: 5
biology: 4.5
IT: 5

and I wanna read it to two others array. In c++ I use:

getline(data, subject[i], ':');

and it's working. I wanna do the same in QT but in subject[i]=in.readLine() I don't know if ther's something alike.

d_stranz
15th June 2017, 21:06
Use readLine() to read the whole line into a QString, then use QString::split() to get the two parts:



QTextStream in( file ); // QFile * file

while ( !in.atEnd() )
{
QString line = in.readLine();
QStringList fields = line.split( ": " );
QString subject = fields[0];
QString gpa = fields[1];

// Now do something with subject and gpa...
}

gusiak96
15th June 2017, 23:11
thanks :D next time I'll use Code tags