Vamsi
Vamsi

Reputation: 87

Wrong values are writing to an array from a CSV file data using c++

I have created a program that reads a CSV file with 2 columns where except first row remaining rows are numerical values. After reading it writes values to 2 different arrays(x[],y[]). My problem is the value are reading perfect but the value that should be at x[0] is at x[-5] and at x[0] there is value of y[4].

below is the data in CSV file

Distance,Time
10,12
57,69
40,54
60,71
90,200
68,76

I have tried different ways to solve this but none of them worked. Please help me if possible.

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
    ifstream data("Data.csv");
    int Count = 0, x[Count+1], y[Count+1], a = 0;//it is behaving differently while removing a=0
    if(data.is_open())
    {

        string line, line1;

        //for first row
        getline(data,line,',');
        cout << line;
        getline(data,line1,'\n');
        cout << " " << line1 << endl;

        //remaining rows with numerical values
        while(data.good())
        {
            getline(data,line,',');
            x[Count] = stoi(line); //converting string to integer
            cout << x[Count];
            getline(data,line1,'\n');
            y[Count] = stoi(line1); //converting string to integer
            cout << " " << y[Count] << endl;
            Count++;
        }

        cout << "   " << Count << endl;
        cout << x[0] << endl;
        cout << y[0] << endl;
    }
    else
    {
       cout << "ERROR OCCURED WHILE OPENING THE FILE" << endl;
    }
}

Upvotes: 0

Views: 191

Answers (2)

darune
darune

Reputation: 10972

Here is a solution using std::istringstream and std::vector<std::pair<int, int>> for storing the result. The only real trick is replacing comma with space so it can be parsed with std::istringstream.

#include <fstream>
#include <vector>
#include <sstream>
//...

ifstream infile("Data.csv");

std::string line;
std::getline(infile, line);//skip header line
std::vector<std::pair<int, int>> coords;
while (std::getline(infile, line))
{
    std::replace(line.begin(), line.end(), ',', ' ');//replace ',' with space
    std::istringstream iss(line);
    int x, y;
    if (!(iss >> x >> y)) { 
        break; // error in format
    }
    coords.emplace_back(x, y);
}

You probably need to better handle the format error situation in real code. Also you could make the header optional if needed (skipping errors on the first line).

Upvotes: 0

slepic
slepic

Reputation: 641

Instead of int[] which has fixed size. Use a standard container like std::vector<int>. And instead of x[Count] = value; use the push_back() method.

#include <vector>
// ...
std::vector<int> x,y;
// ...
x.push_back(stoi(line));
y.push_back(stoi(line1));

int Count = x.size(); //or y.size() as they should be same

You could also use a vector of pairs instead of two vectors std::vector<std::pair<int, int>> or a vector of tuples std::vector<std::tuple<int, int>>

Some error checking should also be added...

Upvotes: 1

Related Questions