Note that there are some explanatory texts on larger screens.

plurals
  1. POAppend to a JSON array in a JSON file on disk, every second using C++
    primarykey
    data
    text
    <p>This is my first post here, so please bear with me.</p> <p>I have searched high and low on the internet for an answer, but I've not been able to resolve my issue, so I have decided to write a post here.</p> <p>I am trying to write(append) to a JSON array on file using C++ and JZON, at intervals of 1 write each second. The JSON file is initially written by a “Prepare” function. Another function is then called each second to a add an array to the JSON file and append an new object to the array every second.</p> <p>I have tried many things, most of which resulted in all sorts of issues. My latest attempt gave me the best results and this is the code that I have included below. However, the approach I took is very inefficient as I am writing an entire array every second. This is having a massive hit on CPU utilisation as the array grows, but not so much on memory as I had first anticipated. </p> <p>What I really would like to be able to do is to append to an existing array contained in a JSON file on disk, line by line, rather than having to clear the entire array from the JSON object and rewriting the entire file, each and every second.</p> <p>I am hoping that some of the geniuses on this website will be able to point me in the right direction.</p> <p>Thank you very much in advance.</p> <p>Here is my code:</p> <pre><code>//Create some object somewhere at the top of the cpp file Jzon::Object jsonFlight; Jzon::Array jsonFlightPath; Jzon::Object jsonCoordinates; int PrepareFlight(const char* jsonfilename) { //...SOME PREPARE FUNCTION STUFF GOES HERE... //Add the Flight Information to the jsonFlight root JSON Object jsonFlight.Add("Flight Number", flightnum); jsonFlight.Add("Origin", originicao); jsonFlight.Add("Destination", desticao); jsonFlight.Add("Pilot in Command", pic); //Write the jsonFlight object to a .json file on disk. Filename is passed in as a param of the function. Jzon::FileWriter::WriteFile(jsonfilename, jsonFlight, Jzon::NoFormat); return 0; } int UpdateJSON_FlightPath(ACFT_PARAM* pS, const char* jsonfilename) { //Add the current returned coordinates to the jsonCoordinates jzon object jsonCoordinates.Add("altitude", pS-&gt; altitude); jsonCoordinates.Add("latitude", pS-&gt; latitude); jsonCoordinates.Add("longitude", pS-&gt; longitude); //Add the Coordinates to the FlightPath then clear the coordinates. jsonFlightPath.Add(jsonCoordinates); jsonCoordinates.Clear(); //Now add the entire flightpath array to the jsonFlight object. jsonFlight.Add("Flightpath", jsonFlightPath); //write the jsonFlight object to a JSON file on disk. Jzon::FileWriter::WriteFile(jsonfilename, jsonFlight, Jzon::NoFormat); //Remove the entire jsonFlighPath array from the jsonFlight object to avoid duplicaiton next time the function executes. jsonFlight.Remove("Flightpath"); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload