2

I already have a YAML file. I want to just change a single value inside that YAML file. I found yaml-cpp library being used widely for parsing/editing yaml files from c++ code. Is there a way I can update single value and leave the rest of the file untouched?

I have a YAML file like this already. And I just want to update the parameter 'non_ros_map_width'

# Config file 
package_name: "auto_mapping_ros"

csv_filepath: "/csv/sequence"

# Non ROS Map Values
non_ros_map_width : 1000

I tried runnning using yaml-cpp and updated it from the cpp code and I got this:

package_name: !<!> auto_mapping_ros
non_ros_map_height: 1355
csv_filepath: !<!> /csv/sequence

The values seem to be intact. I am not sure about the strings. but my comments have disappeared. Is there a way to just update the single value and not touch the rest of the file.

My code snippet:

YAML::Node node, _baseNode = YAML::LoadFile(auto_mapping_yaml_path); // gets the root node
_baseNode["non_ros_map_width"] = 1355; // edit one of the nodes
std::ofstream fout(auto_mapping_yaml_path);
fout << _baseNode; // dump it back into the file
Cosmin
  • 21,216
  • 5
  • 45
  • 60
yasht
  • 195
  • 1
  • 15

1 Answers1

1

From the YAML documentation https://yaml.org/spec/1.2/spec.html#id2767100:

Comments are a presentation detail and must not have any effect on the serialization tree or representation graph.

YAML will remove the comments by design.

A solution would be to edit the file manually: C++ overwriting data in a file at a particular position. But you would also have to parse the file manually.

Cosmin
  • 21,216
  • 5
  • 45
  • 60