JsonCpp Writing to Json File

I have a Config file with the following contents:

{ "ip": "127.0.0.1", "heartbeat": "1", "ssl": "False", "log_severity": "debug", "port":"9999" } 

I used JsonCpp to read the contents of the above configuration file. Reading the contents of the Config file is fine, but writing the contents to the configuration file is not performed. I have the following code:

 #include <json/json.h> #include <json/writer.h> #include <iostream> #include <fstream> int main() { Json::Value root; // will contains the root value after parsing. Json::Reader reader; Json::StyledStreamWriter writer; std::ifstream test("C://SomeFolder//lpa.config"); bool parsingSuccessful = reader.parse( test, root ); if ( !parsingSuccessful ) { // report to the user the failure and their locations in the document. std::cout << "Failed to parse configuration: "<< reader.getFormattedErrorMessages(); } std::cout << root["heartbeat"] << std::endl; std::cout << root << std::endl; root["heartbeat"] = "60"; std::ofstream test1("C://SomeFolder//lpa.config"); writer.write(test1,root); std::cout << root << std::endl; return 0; } 

The code prints the correct output in the console, however the configuration file is empty when this code is executed. How to make this code work?

+6
source share
2 answers

All you have to do is explicitly close the input stream

 test.close(); // ADD THIS LINE std::ofstream test1("C://LogPointAgent//lpa.config"); writer.write(test1,root); std::cout << root << std::endl; return 0; 
+5
source

All you have to do is close the open file.

test1.close ();

+2
source

Source: https://habr.com/ru/post/979631/


All Articles