我正在尝试替换配置文件中的值。它可以在文本编辑器中编辑。
[MODEL] ...
Latitude = 1.034 Longitude = 110.58334 ...
如何搜索关键字“Latitude”并将“1.034”替换为其他值? (例如1.04)
编辑:谢谢你的选票。我在下面附上了我的代码:
QFile fileReadModels(CP1);
fileReadModels.open(QIODevice::ReadOnly | QIODevice::Text);
QTextStream inModels(&fileReadModels);
while(!inModels.atEnd())
{
QString lineModels = inModels.readLine();
if(lineModels.isNull())
{
break;
}
else
{
strListInModels.append(lineModels);
}
}
fileReadModels.close();
int startFlag = 0;
int stopFlag = 0;
for(int i = 1; i <= strListInModels.count(); i++)
{
if(strListInModels[i].contains("Latitude", Qt::CaseInsensitive) == 1)
{
//! get start position
startFlag = i - 1;
//! get stop position
stopFlag = i + 2;
break;
}
}
//! get data from start till start position
for(int x = 0; x <= startFlag; x++)
{
strListOutModels << strListInModels[x];
}
//! insert in Ownship1 lat/lon
QString os1LatStr = " Latitude = " + os1Lat;
QString os1LonStr = " Longitude = " + os1Lon;
strListOutModels << os1LatStr;
strListOutModels << os1LonStr;
//! get data from stop postion till end
for(int y = stopFlag; y < strListInModels.count(); y++)
{
strListOutModels << strListInModels[y];
}
//! write to file
QFile fileWriteModels(CP1);
fileWriteModels.open(QIODevice::ReadWrite | QIODevice::Truncate);
QTextStream outModels(&fileWriteModels);
QString qStrModels = strListOutModels.join("\r");
outModels << qStrModels;
fileWriteModels.close();
答案 0 :(得分:2)
您可以使用QSettings功能
<强> location.ini 强>
[MODEL]
Latitude = 1.034
Longitude = 110.58334
您可以使用QSetting修改数据
//Access your datafile
QSettings settings("location.ini", QSettings::IniFormat);
//Read Data
QString sLatitude = settings.value("MODEL/Latitude").toString();
//Write Data
settings.setValue("MODEL/Latitude", "18.55");