READ / WRITE ini files

HI,

I want to save data on an SD card. I use an INI file... with the library of Steve Marple (https://github.com/stevemarple/IniFile) It is perfect to read the data But how can I change something in this INI file.. I see that this library can not do that.

Is there anyone who can help me to implement this 'write' function??

I see that this library can not do that.

Why not? Once you've read all the data, you have everything you need to re-write the file containing exactly the same data.

Making changes, and writing the file using different data, is trivial.

Where IS your code?

PaulS:
Making changes, and writing the file using different data, is trivial.

Doesn’t take this a long time to write a whole new file… I though it can be done by just write the new variabele to the new key in the INI-file.

I can read ONE key from the file, so it should be possible to write (change) ONE key in the same file…

PaulS:
Where IS your code?

This is the INI file:

[item1]
text = Light Kitchen
type = 1

[item2]
text = Light bedroom
type = 0

[item3]
text = test3
type = 4

[Options]
tempOutside = 1
tempInside = 1

This is the Arduino Code:

#include <IniFile.h>

#define SD_SELECT 4
#define ETHERNET_SELECT 53

void printErrorMessage(uint8_t e, bool eol = true)
{
  switch (e) {
  case IniFile::errorNoError:
    Serial.print("no error");
    break;
  case IniFile::errorFileNotFound:
    Serial.print("file not found");
    break;
  case IniFile::errorFileNotOpen:
    Serial.print("file not open");
    break;
  case IniFile::errorBufferTooSmall:
    Serial.print("buffer too small");
    break;
  case IniFile::errorSeekError:
    Serial.print("seek error");
    break;
  case IniFile::errorSectionNotFound:
    Serial.print("section not found");
    break;
  case IniFile::errorKeyNotFound:
    Serial.print("key not found");
    break;
  case IniFile::errorEndOfFile:
    Serial.print("end of file");
    break;
  case IniFile::errorUnknownError:
    Serial.print("unknown error");
    break;
  default:
    Serial.print("unknown error value");
    break;
  }
  if (eol)
    Serial.println();
}

void setup()
{
  // Configure all of the SPI select pins as outputs and make SPI
  // devices inactive, otherwise the earlier init routines may fail
  // for devices which have not yet been configured.
  pinMode(SD_SELECT, OUTPUT);
  digitalWrite(SD_SELECT, HIGH); // disable SD card
  
  pinMode(ETHERNET_SELECT, OUTPUT);
  digitalWrite(ETHERNET_SELECT, HIGH); // disable Ethernet

  const size_t bufferLen = 80;
  char buffer[bufferLen];

  const char *filename = "config.ini";
  Serial.begin(9600);
  SPI.begin();
  if (!SD.begin(SD_SELECT))
    while (1)
      Serial.println("SD.begin() failed");
  
  IniFile ini(filename);
  if (!ini.open()) {
    Serial.print("ERROR - Ini file ");
    Serial.print(filename);
    Serial.println(" does not exist");
    // Cannot do anything else
    while (1)
      ;
  }
  Serial.print("SUCCESS - Found ");
  Serial.print(filename);
  Serial.println(" file.");

  // Check the file is valid. This can be used to warn if any lines
  // are longer than the buffer.
  if (!ini.validate(buffer, bufferLen)) {
    Serial.print("ini file ");
    Serial.print(ini.getFilename());
    Serial.print(" not valid: ");
    printErrorMessage(ini.getError());
    // Cannot do anything else
    while (1)
      ;
  }
  
  // Fetch a value from a key which is present
  if (ini.getValue("item1", "text", buffer, bufferLen)) {
    Serial.print("section 'item1' has an entry 'text' with value ");
    Serial.println(buffer);
  }
  else {
    Serial.print("Could not read 'text' from section 'item1', error was ");
    printErrorMessage(ini.getError());
  }
  if (ini.getValue("item1", "type", buffer, bufferLen)) {
    Serial.print("section 'item1' has an entry 'type' with value ");
    Serial.println(buffer);
  }
  else {
    Serial.print("Could not read 'type' from section 'item1', error was ");
    printErrorMessage(ini.getError());
  }
  
  
  
    if (ini.getValue("item2", "text", buffer, bufferLen)) {
    Serial.print("section 'item2' has an entry 'text' with value ");
    Serial.println(buffer);
  }
  else {
    Serial.print("Could not read 'text' from section 'item1', error was ");
    printErrorMessage(ini.getError());
  }
    if (ini.getValue("item2", "type", buffer, bufferLen)) {
    Serial.print("section 'item2' has an entry 'type' with value ");
    Serial.println(buffer);
  }
  else {
    Serial.print("Could not read 'type' from section 'item2', error was ");
    printErrorMessage(ini.getError());
  }
  
    if (ini.getValue("item3", "text", buffer, bufferLen)) {
    Serial.print("section 'item3' has an entry 'text' with value ");
    Serial.println(buffer);
  }
  else {
    Serial.print("Could not read 'text' from section 'item1', error was ");
    printErrorMessage(ini.getError());
  }
    if (ini.getValue("item3", "type", buffer, bufferLen)) {
    Serial.print("section 'item3' has an entry 'type' with value ");
    Serial.println(buffer);
  }
  else {
    Serial.print("Could not read 'type' from section 'item3', error was ");
    printErrorMessage(ini.getError());
  }
}



void loop(){
//the rest of the code
}

Will changing the value change the length of the INI file? If so, making the change isn't a simple matter of just overwriting what's already there. If you think of a file in terms of it being a series of bytes written to a storage device, it becomes easier to understand why you just write out the whole thing.