1

I'm trying to read an INI file and write another INI file with the sections order changed.

Does Windows API have tools for this kind of INI file processing? Are there open-source solutions that are preferred? Or should I try to attempt to parse it manually?

zenpoy
  • 19,490
  • 9
  • 60
  • 87
  • GetPrivateProfileSectionNames() returns the section names, GetPrivateProfileSection() returns a section. Changing the order is pretty pointless. And never use the api for reading them, they are horribly expensive. 50 milliseconds to read *one* setting. – Hans Passant Oct 21 '12 at 09:58
  • @HansPassant Thanks for the comment. I'm going to go over the file once just to create an ini file for another program where the order is critical. So with GetPrivateProfileSectionNames() I'll get an array of names and will be able to change the order, and then go over the section names and write the data to the new file... – zenpoy Oct 21 '12 at 10:02
  • Hard to imagine you need to do this more than once btw. Why don't you just use a text editor? – Hans Passant Oct 21 '12 at 10:09
  • @HansPassant :) every client running my program does this once, on init, and the order is based the user's environment. This is why I can't use text-editor. – zenpoy Oct 21 '12 at 10:15
  • @zenpoy: if the order of the sections is important, then it is not a true INI file. Since the order is important, you can use the Win32 API PrivateProfile functions to read from the source file but DO NOT use them to write to the destination file. Write to the file yourself, then you can order it any way you want. – Remy Lebeau Oct 21 '12 at 20:36
  • @HansPassant I ended up using `GetPrivateProfileSectionNames()` and `WritePrivateProfileSection()`. and it's working great. You can post it as an answer and I'll accept it. – zenpoy Oct 22 '12 at 14:04

2 Answers2

2

Windows has its own INI functions for C. But since you mentioned C++ try Feather INI. Program Options is another from Boost.

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
1

As noted in the comment, you can use winapi functions to do this. GetPrivateProfileSectionNames returns the names of the sections in the .ini file. Which you can then iterate and call GetPrivateProfileSection() to get the content of each section.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536