PDA

View Full Version : Removeing a group from INI file



deepal_de
12th August 2011, 13:04
Hello,
i have a INI file in following format.



[sys_1]
val1 = 10
val2 = 23
val3 = 23

[sys_2]
val1 = 56
val2 = 2
val3 = 27

[sys_3]
val1 = 84
val2 = 65
val3 = 12


what i need to do is remove sys_2 completely
so it will be like this



[sys_1]
val1 = 10
val2 = 23
val3 = 23


[sys_3]
val1 = 84
val2 = 65
val3 = 12



and if u can please post a example code , thanks .. :)

qlands
12th August 2011, 14:57
Hi,

Maybe with this:



QFile file("in.ini");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream in(&file);
QString currentGroup;
QStringList temp;
int pos;
int pos2;
QString grupToDelete;
grupToDelete = "SomeThing";
while (!in.atEnd())
{
QString line = in.readLine();
if (line.contains("[") && line.contains("]")) //If it is a group
{
pos = line.indexOf("[");
pos2 = line.indexOf("]");
currentGroup = line.mid(pos+1,pos2-1); //Get the group
}
if (currentGroup != grupToDelete) //If the line is not the group to delete
{
temp.append(line); //Move the lines to temp
}
}
//Temp will now have all the lines but those to delete
//So save temp to out.ini
QFile ofile("out.ini");
if (!ofile.open(QIODevice::WriteOnly | QIODevice::Text))
return;
QTextStream out(&ofile);
for (pos = 0; pos <= temp.count()-1;pos++)
out << temp[pos] << "\n";

QFile::remove("ini.ini"); //Remove the old file
QFile::rename("out.ini","in.ini"); //Rename out to in


I have no clue if an INI group can have subgroups.... If so... It will not remove any subgroups (like a tree) just the specified in grupToDelete.. You can make it a function so you can delete more than one group though.
Test it first!!!!

Lesiok
12th August 2011, 15:53
Just use QSettings::remove