Robert Kulagowski
31st May 2012, 21:55
QT4.8.1
The following is a code snippet. It reads in a text file where each line contains json in a non-pretty-print format.
The intent is to read in each line, extract a key called "prog_id", then store a QMap using prog_id as the key.
QString filename = "/tmp/10021_sched.txt";
QFile inputfile(filename);
if (!inputfile.open(QIODevice::ReadOnly))
{
qDebug() << "Couldn't open filename: " << filename;
return false;
}
QJson::Parser parser;
bool ok;
QMap<QString, QString> map;
QMapIterator<QString, QString> i(map);
QString line;
QString key;
while (!inputfile.atEnd())
{
// Read everything into a QMap first. The largest data file only has around 4000 lines, so not huge.
line = inputfile.readLine();
QVariantMap result = parser.parse(line.toLocal8Bit(), &ok).toMap(); //json parser wants QByteArray
if (!ok)
{
printf("line %d: %s\n", parser.errorLine(), parser.errorString().toUtf8().data());
return false;
}
// Store each line read in from the file using prog_id as the key.
//qDebug() << "progid is " << result["prog_id"];
//qDebug() << "line is " << line;
key = result["prog_id"].toString();
// The next line works.
qDebug() << "key is: " << key;
map[key] = line;
}
inputfile.close();
// Added this because the iterator didn't seem to work.
i.toFront();
// This prints.
printf("Starting loop\n");
while (i.hasNext())
{
// This doesn't print.
printf("Inside loop\n");
i.next();
// I never get any output inside the while
qDebug() << i.key() << ": " << i.value();
}
// This works.
foreach (QString value, map)
{
qDebug() << "value is: " << value;
}
Snippet is obviously kludged together, but I'm not understanding why the while "Inside Loop" never seems to execute.
The following is a code snippet. It reads in a text file where each line contains json in a non-pretty-print format.
The intent is to read in each line, extract a key called "prog_id", then store a QMap using prog_id as the key.
QString filename = "/tmp/10021_sched.txt";
QFile inputfile(filename);
if (!inputfile.open(QIODevice::ReadOnly))
{
qDebug() << "Couldn't open filename: " << filename;
return false;
}
QJson::Parser parser;
bool ok;
QMap<QString, QString> map;
QMapIterator<QString, QString> i(map);
QString line;
QString key;
while (!inputfile.atEnd())
{
// Read everything into a QMap first. The largest data file only has around 4000 lines, so not huge.
line = inputfile.readLine();
QVariantMap result = parser.parse(line.toLocal8Bit(), &ok).toMap(); //json parser wants QByteArray
if (!ok)
{
printf("line %d: %s\n", parser.errorLine(), parser.errorString().toUtf8().data());
return false;
}
// Store each line read in from the file using prog_id as the key.
//qDebug() << "progid is " << result["prog_id"];
//qDebug() << "line is " << line;
key = result["prog_id"].toString();
// The next line works.
qDebug() << "key is: " << key;
map[key] = line;
}
inputfile.close();
// Added this because the iterator didn't seem to work.
i.toFront();
// This prints.
printf("Starting loop\n");
while (i.hasNext())
{
// This doesn't print.
printf("Inside loop\n");
i.next();
// I never get any output inside the while
qDebug() << i.key() << ": " << i.value();
}
// This works.
foreach (QString value, map)
{
qDebug() << "value is: " << value;
}
Snippet is obviously kludged together, but I'm not understanding why the while "Inside Loop" never seems to execute.