Search code examples
c++inipoco-libraries

How to write changes to ".ini" file using POCO libraries?


I'm trying to make changes to a ".ini" file using Poco::Util::IniFileConfiguration. I have the following example ini file:

[Test]
IP = 192.168.1.1

I want to be able to write a new IP to the file. What I have so far is:

#include "Poco/Util/IniFileConfiguration.h"
#include <iostream>

int main( int argc, char *argv[] ) {
  Poco::AutoPtr<Poco::Util::IniFileConfiguration> pConf( new Poco::Util::IniFileConfiguration( "file.ini" ) );
  if ( pConf->has( "Test.IP" ) ) {
    try {
      std::cout << pConf->getString( "Test.IP" ) << std::endl;
      pConf->setString( "Test.IP", "127.0.0.1" );
      std::cout << pConf->getString( "Test.IP" ) << std::endl;
      // TODO Make changes permanent; write to file!
    } catch ( Poco::SyntaxException& e ) {
      std::cerr << "writeValue: " << e.displayText() << std::endl;
      return -1;
    }
  }
  return 0;
}

This code prints the following:

192.168.1.1
127.0.0.1

The IP is changed on runtime but it doesn't write the changes to disk (file.ini is not changed). Is there an easy way to achieve this?

Thank you!


Solution

  • It was strange but currently Poco IniConfigurationFile is readonly and can't be save. Proof on this presentation.

    So, if you want to work with ini files you need to write own implementation to save PocoIniConfiguration or use another library.