Home > Enterprise >  Trying to run a count so I know when the last row of data is written so I can output a ']'
Trying to run a count so I know when the last row of data is written so I can output a ']'

Time:12-15

I want my output file to look like this:

image

However, no matter what I try, it looks like this:

image

I can't get my comma condition to work for the output. I've tried to use eof, counts, etc but I'm not really sure where to go.

I've tried looking at other posts, but I either can't find one linked, or I don't actually understand it.

#include <iostream>
#include <fstream> //ofstream declared in this header file
#include <string>
#include <sstream>
#include <vector>

using namespace std;

//Creates Structure For Columns
struct InputFile
{
    string Date;
    string Value;
    string SignalStrength;
    string Voltage;
};

int main()
{
    ifstream input;
    string Row;
    string Column;
    int count = 0;
    int count2 = 0;

    //Stores Data From Structure As Vector
    vector<InputFile> InputDataStored;
    input.open("Temperature.csv");

    if (input.fail())
    {
        cerr << "File does not exist. Exiting" << endl; //cerr is cout for errors
        return 1; //This could be used as an error code
    }

    if (!input)
    {
        cerr << "File could not be opened." << endl;
    }

    while (getline(input, Row))   //Remove top line output from sensor data when opened in Notepad
    {
        getline(input, Row);  // read an entire row and store it in a string variable 'line'
        stringstream ss{ Row };  // used for breaking words
        vector<string> Columns;  // creates a temporary vector of strings
        while (getline(ss, Column, ','))  // read an entire row and store it in a string variable 'column'
        {
            Columns.push_back(Column);  // add all the data of a row to the temporary vector
            count  ;
        }
        //InputFile t{};  // convert string to struct types
        InputFile t;

        if (Row.empty())
            continue; //  if it is a blank row, ignore it
        else
        t.Date = Columns[1];
        t.Value = Columns[2];
        t.SignalStrength = Columns[4];
        t.Voltage = Columns[5];
        InputDataStored.push_back(t);  // add all the data of the new row to a vector
        count2  ;
        cout << t.Date << "         " << t.Value << "         " << t.SignalStrength << "         " << t.Voltage << endl;
    }

    input.close();

    ofstream output;
    output.open("SensorData.json");

    if (!output)
    {
        cerr << "File could not be opened." << endl;
    }
    int JSONcount = 0;

    output << "[";

    for (InputFile t : InputDataStored)
    {

        JSONcount  ;
        output << "{" << endl;
        output << "\"Date\": \"" << t.Date << "\"" << endl;
        output << "\"Temperature\": " << t.Value << endl;
        output << "\"Signal_strength\": " << t.SignalStrength << endl;
        output << "\"Voltage\": " << t.Voltage << endl;

        if (count2 >= JSONcount)
            output << "}]" << endl;
        else
            output << "}," << endl;      
    }

    output << JSONcount << endl;
    output << count << endl;
    output << count2;
    output.close();
}

CodePudding user response:

There are multiple mistakes in your code. Try something more like this instead:

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>

using namespace std;

struct InputFile
{
    string Date;
    string Value;
    string SignalStrength;
    string Voltage;
};

int main()
{
    vector<InputFile> InputDataStored;
    string Line, Column;
    int count = 0, count2 = 0;

    ifstream input("Temperature.csv");
    if (!input)
    {
        cerr << "Input file could not be opened. Exiting" << endl;
        return 1;
    }

    getline(input, Line);   //Remove top line output from sensor data when opened in Notepad

    while (getline(input, Line))  // read an entire row and store it in a string variable 'line'
    {
        istringstream iss{ Line };
        if (Line.empty())
            continue;

        vector<string> Columns;
        while (getline(iss, Column, ','))
        {
            Columns.push_back(Column);
              count;
        }

        InputFile t;
        t.Date = Columns[1];
        t.Value = Columns[2];
        t.SignalStrength = Columns[4];
        t.Voltage = Columns[5];

        InputDataStored.push_back(t);
          count2;

        cout << t.Date << "         " << t.Value << "         " << t.SignalStrength << "         " << t.Voltage << endl;
    }

    input.close();

    ofstream output("SensorData.json");
    if (!output)
    {
        cerr << "Output file could not be opened. Exiting" << endl;
        return 1;
    }

    int JSONcount = 0;

    output << "[";
    for (const auto &t : InputDataStored)
    {
          JSONcount;
        if (JSONcount > 1)
            output << "," << endl;

        output << "{" << endl;
        output << "\"Date\": \"" << t.Date << "\"" << endl;
        output << "\"Temperature\": " << t.Value << endl;
        output << "\"Signal_strength\": " << t.SignalStrength << endl;
        output << "\"Voltage\": " << t.Voltage << endl;
        output << "}";
    }
    output << "]" << endl;

    output << JSONcount << endl;
    output << count << endl;
    output << count2;

    output.close();

    return 0;
}
  • Related