I have a json-file
{
"X": "3.5",
"Y": "4.4"
}
and I need to change the value of the variable x (increase it by 0.5)
{
"X": "4.0",
"Y": "4.4"
}
I would like to read the file, change the variable and write the file. I have problems writing (modifying) a file.
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <json/json.h>
int main(){
std::string mConfigFileName = "./test.json";
std::ifstream configFile(mConfigFileName);
std::stringstream ss;
ss << configFile.rdbuf();
std::string configString = ss.str();
Json::Value root;
Json::Reader reader;
bool ok = reader.parse(configString, root );
double X = std::stod(root["X"].asString().c_str());
std::cout << "X = "<< X << std::endl;
X += 0.5;
// how to save changes ???
}
g++ -I/usr/include/jsoncpp/ test.cpp -ljsoncpp