title: How to modify a value with yaml-cpp?
body: <p>I'm trying to make configuration file for my application, for which I'm using <code>yaml-cpp</code> library to generate a configuration and modify when user changes changes some setting or something in the application. I have a separate class set up for this, the constructor generates the <code>yaml</code> file which looks like this,</p>
  <pre><code>Serializer::Serializer(const std::string&amp; filepath)
  {
      std::ifstream ifin(filepath);
  
      if (!ifin)
      {
          emitter &lt;&lt; YAML::Comment(&quot;Hello&quot;);
          emitter &lt;&lt; YAML::BeginDoc;
          emitter &lt;&lt; &quot;This is the configuration file for the Sample Browser,&quot;
                  &lt;&lt; YAML::Newline;
          emitter &lt;&lt; &quot;feel free to edit this file as needed&quot;;
          emitter &lt;&lt; YAML::EndDoc;
  
          emitter &lt;&lt; YAML::BeginMap;
  
          emitter &lt;&lt; YAML::Newline &lt;&lt; YAML::Key &lt;&lt; &quot;Window&quot;;
          emitter &lt;&lt; YAML::BeginMap;
          emitter &lt;&lt; YAML::Key &lt;&lt; &quot;SizeW&quot; &lt;&lt; YAML::Value &lt;&lt; &quot;1280&quot;;
          emitter &lt;&lt; YAML::Key &lt;&lt; &quot;SizeH&quot; &lt;&lt; YAML::Value &lt;&lt; &quot;720&quot;;
          emitter &lt;&lt; YAML::EndMap &lt;&lt; YAML::Newline;
  
          emitter &lt;&lt; YAML::Newline &lt;&lt; YAML::Key &lt;&lt; &quot;Media&quot;;
          emitter &lt;&lt; YAML::BeginMap;
          emitter &lt;&lt; YAML::Key &lt;&lt; &quot;Autoplay&quot; &lt;&lt; YAML::Value &lt;&lt; &quot;false&quot;;
          emitter &lt;&lt; YAML::Key &lt;&lt; &quot;Loop&quot; &lt;&lt; YAML::Value &lt;&lt; &quot;false&quot;;
          emitter &lt;&lt; YAML::Key &lt;&lt; &quot;Muted&quot; &lt;&lt; YAML::Value &lt;&lt; &quot;false&quot;;
          emitter &lt;&lt; YAML::EndMap &lt;&lt; YAML::Newline;
  
          emitter &lt;&lt; YAML::Newline &lt;&lt; YAML::Key &lt;&lt; &quot;Display&quot;;
          emitter &lt;&lt; YAML::BeginMap;
          emitter &lt;&lt; YAML::Key &lt;&lt; &quot;Font&quot;;
          emitter &lt;&lt; YAML::BeginMap;
          emitter &lt;&lt; YAML::Key &lt;&lt; &quot;Family&quot; &lt;&lt; YAML::Value &lt;&lt; &quot;Sans&quot;;
          emitter &lt;&lt; YAML::Key &lt;&lt; &quot;Size&quot; &lt;&lt; YAML::Value &lt;&lt; &quot;10&quot;;
          emitter &lt;&lt; YAML::EndMap;
          emitter &lt;&lt; YAML::EndMap &lt;&lt; YAML::Newline;
  
          emitter &lt;&lt; YAML::Newline &lt;&lt; YAML::Key &lt;&lt; &quot;Import_dir&quot;;
          emitter &lt;&lt; YAML::BeginMap;
          emitter &lt;&lt; YAML::Key &lt;&lt; &quot;AutoImport&quot; &lt;&lt; YAML::Value &lt;&lt; &quot;false&quot;;
          emitter &lt;&lt; YAML::Key &lt;&lt; &quot;Directory&quot; &lt;&lt; YAML::Value &lt;&lt; &quot;/home/apoorv&quot;;
          emitter &lt;&lt; YAML::EndMap &lt;&lt; YAML::Newline;
  
          emitter &lt;&lt; YAML::EndMap;
  
          std::ofstream ofout(filepath);
          ofout &lt;&lt; emitter.c_str();
      }
      else
      {
          wxLogDebug(&quot;Config file already exists! Skipping..&quot;);
      }
  }
  </code></pre>
  <p>and the output of this file looks like,</p>
  <pre><code># Hello
  ---
  This is the configuration file for the Sample Browser,
  feel free to edit this file as needed
  ...
  
  Window:
    SizeW: 1280
    SizeH: 720
  
  Media:
    Autoplay: false
    Loop: false
    Muted: false
  
  Display:
    Font:
      Family: &quot;Sans&quot;
      Size: &quot;10&quot;
  
  Import_dir:
    AutoImport: false
    Directory: &quot;/home/apoorv/&quot;
  </code></pre>
  <p>I want that when user changes say font, the related key value also changes with the selected option. I tried making a function that should do this like,</p>
  <pre><code>void Serializer::SerializeDisplaySettings(const std::string&amp; filepath, wxFont&amp; font)
  {
      YAML::Emitter out;
  
      std::string fontFace = font.GetFaceName().ToStdString();
      int fontSize = font.GetPointSize();
  
      std::ifstream ifin(filepath);
  
      try
      {
          YAML::Node config = YAML::LoadAllFromFile(filepath)[1];
  
          auto display = config[&quot;Display&quot;];
  
          if (auto fontSetting = display[&quot;Font&quot;])
          {
              wxLogDebug(&quot;Changing font settings&quot;);
              wxLogDebug(&quot;Font face: %s&quot;, fontFace);
              wxLogDebug(&quot;Font size: %d&quot;, fontSize);
  
              out &lt;&lt; YAML::Key &lt;&lt; fontSetting[&quot;Family&quot;] &lt;&lt; YAML::Value &lt;&lt; fontFace;
              out &lt;&lt; YAML::Key &lt;&lt; fontSetting[&quot;Size&quot;] &lt;&lt; YAML::Value &lt;&lt; fontSize;
  
              std::ofstream ofout(filepath);
              ofout &lt;&lt; out.c_str();
          }
          else
          {
              wxLogDebug(&quot;Error! Cannot fetch values.&quot;);
          }
      }
  
      catch(const YAML::ParserException&amp; ex)
      {
          std::cout &lt;&lt; ex.what() &lt;&lt; std::endl;
      }
  }
  </code></pre>
  <p>but this deletes the whole and fills just font name and size. How do I modify those values without changing/deleting whole file?</p>