Results 1 to 3 of 3

Thread: Removeing a group from INI file

  1. #1
    Join Date
    Jan 2011
    Location
    Sri Lanaka
    Posts
    64
    Thanks
    39
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Post Removeing a group from INI file

    Hello,
    i have a INI file in following format.

    Qt Code:
    1. [sys_1]
    2. val1 = 10
    3. val2 = 23
    4. val3 = 23
    5.  
    6. [sys_2]
    7. val1 = 56
    8. val2 = 2
    9. val3 = 27
    10.  
    11. [sys_3]
    12. val1 = 84
    13. val2 = 65
    14. val3 = 12
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. [sys_1]
    2. val1 = 10
    3. val2 = 23
    4. val3 = 23
    5.  
    6.  
    7. [sys_3]
    8. val1 = 84
    9. val2 = 65
    10. val3 = 12
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Removeing a group from INI file

    Hi,

    Maybe with this:

    Qt Code:
    1. QFile file("in.ini");
    2. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    3. return;
    4. QTextStream in(&file);
    5. QString currentGroup;
    6. int pos;
    7. int pos2;
    8. QString grupToDelete;
    9. grupToDelete = "SomeThing";
    10. while (!in.atEnd())
    11. {
    12. QString line = in.readLine();
    13. if (line.contains("[") && line.contains("]")) //If it is a group
    14. {
    15. pos = line.indexOf("[");
    16. pos2 = line.indexOf("]");
    17. currentGroup = line.mid(pos+1,pos2-1); //Get the group
    18. }
    19. if (currentGroup != grupToDelete) //If the line is not the group to delete
    20. {
    21. temp.append(line); //Move the lines to temp
    22. }
    23. }
    24. //Temp will now have all the lines but those to delete
    25. //So save temp to out.ini
    26. QFile ofile("out.ini");
    27. if (!ofile.open(QIODevice::WriteOnly | QIODevice::Text))
    28. return;
    29. QTextStream out(&ofile);
    30. for (pos = 0; pos <= temp.count()-1;pos++)
    31. out << temp[pos] << "\n";
    32.  
    33. QFile::remove("ini.ini"); //Remove the old file
    34. QFile::rename("out.ini","in.ini"); //Rename out to in
    To copy to clipboard, switch view to plain text mode 

    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!!!!
    Last edited by qlands; 12th August 2011 at 14:01.

  3. The following user says thank you to qlands for this useful post:

    deepal_de (26th August 2011)

  4. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Removeing a group from INI file


  5. The following user says thank you to Lesiok for this useful post:

    deepal_de (26th August 2011)

Similar Threads

  1. How to group items??
    By sudhansu in forum Qt Programming
    Replies: 8
    Last Post: 15th January 2010, 11:06
  2. Group Box
    By bmn in forum Qt Programming
    Replies: 1
    Last Post: 5th May 2008, 11:36
  3. group box
    By me_here_me in forum Qt Programming
    Replies: 3
    Last Post: 7th September 2007, 19:28
  4. QAction group
    By mickey in forum Newbie
    Replies: 1
    Last Post: 12th July 2006, 19:15
  5. How to add a Action Group?
    By hcostelha in forum Qt Tools
    Replies: 2
    Last Post: 18th January 2006, 17:25

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.