Hi Community,
I need to read and parse a file containing key-value pairs (only 1 line):
-t int_value -d string_value -t int_value -d string_value
The number and order of -d or -t keys is arbitrary
example:
-t 12 -t 49 -d "option_x" -d "option_y"
I don't know the number of pairs -t value or -d value.
Any suggestion on the approach I can use?
Thank you
Added after 10 minutes:
I tried this approach
QFile parametersFile
(parametersFilePath
);
{
if (!line.isEmpty())
{
for (int i = 0; i < fields.count();)
{
if (fields[i] == '-t')
{
map.insert("EDIT_PARAMETER", fields[i+1]);
}
else if (fields[i] == '-d')
{
map.insert("COMBO_PARAMETER", fields[i+1]);
}
i+=2;
}
}
return;
}
QFile parametersFile(parametersFilePath);
if (parametersFile.open(QIODevice::ReadOnly | QIODevice::Text))
{
QTextStream stream(¶metersFile);
QString line = stream.readLine();
if (!line.isEmpty())
{
QStringList fields = line.split(' ');
for (int i = 0; i < fields.count();)
{
if (fields[i] == '-t')
{
map.insert("EDIT_PARAMETER", fields[i+1]);
}
else if (fields[i] == '-d')
{
map.insert("COMBO_PARAMETER", fields[i+1]);
}
i+=2;
}
}
return;
}
To copy to clipboard, switch view to plain text mode
But it's not working as expected
Bookmarks