I am looking for help in creating a dynamically expanding array to import data from a .csv file. I do not want to have to see how large the file is and edit the variable in the source code/prompt the user, I just want the data to be imported then manipulated in various ways. First, my code as-is:
#include <fstream>
#include <sstream>
#include <iostream>
int main()
{
//declare variables and arrays
long rows = 170260;
int cols = 5;
double **rawData = new double*[rows]; //on heap because of size
for(long pi = 0; pi < rows; ++pi) //create an array of pointers
{
rawData[pi] = new double[cols];
}
char buff[200];
double deltaT;
double carDeltaV;
double *carV = new double[rows]; //on heap because of size
//import raw data
std::cout << "Importing filedata.csv...";
std::ifstream rawInput("filedata.csv");
for(long r = 0; r < rows; ++r)
{
rawInput.getline(buff, 200);
std::stringstream ss(buff);
for(int c = 0; c < cols; ++c)
{
ss.getline(buff, 40, ',');
rawData[r][c] = atof(buff);
}
}
std::cout << "Done." << std::endl;
//create speed matrix
carV[0] = 0;
std::cout << std::endl << "Creating speed matrix...";
for (long i = 1; i < rows; ++i)
{
deltaT = rawData[i][0] - rawData[i-1][0];
carDeltaV = rawData[i-1][3] * deltaT;
carV[i] = carDeltaV + carV[i-1];
}
std::cout << "Done." << std::endl;
//write data to csv file
std::cout << std::endl << "Writing data to file...";
std::ofstream outputData;
outputData.open("outputdata.csv");
for(long r = 0; r < rows; ++r)
{
outputData << rawData[r][0] << "," << rawData[r][3]/.00981 << ",";
outputData << carV[r] << std::endl;
}
outputData.close();
std::cout << "Done." << std::endl;
//delete pointers
std::cout << std::endl << "Clearing memory...";
for(long pj = 0; pj < rows; ++pj)
{
delete [] rawData[pj];
}
delete [] rawData;
delete [] carV;
std::cout << "Done." << std::endl;
std::cin.get();
return 0;
}
Note: The amount of colums will always be 5. The rows are my unknown. An example of what I will be importing can be seen below:
0.001098633,0.011430004,0.002829004,-0.004371409,0.00162947
0.001220703,0.00606778,0.001273052,0.003497127,0.002359922
0.001342773,0.003104446,-0.000848701,0.012385657,-0.008119254
There is more to it, but this should be enough to understand what I am trying to accomplish. I have read up on vectors a bit, but the concept of a vector-of-vectors is a bit confusing to me, and I have tried to implement it with no success. Also, I read that a deque might be what I am looking for? I have no experience with those, and it seems to me that it may be overkill for my application since I am only appending in one direction to an array of data.
Disclaimer: I am pretty much a novice at C++, so if there are any concepts that you feel would be above my level of skill please let me know so I can read up on it.
Any advice?
Edit: By request, this is how I tried to do this with vectors.
std::vector<double> rawDataRow;
std::vector< std::vector<double> > rawDataMatrix;
//import raw data loop
std::ifstream rawInput("test.csv");
for(int i = 1; i > 0; ) {
rawInput.getline(buff, 200);
std::stringstream ss(buff);
for(int c = 0; c < cols; ++c) {
ss.getline(buff, 40, ',');
value = atof(buff);
rawDataRow.push_back(value);
std::cout << rawDataRow[0] << std::endl;
}
timeDiff = timeAfter - timeBefore;
timeBefore = timeAfter;
timeAfter = rawDataRow[0];
rawDataMatrix.push_back(rawDataRow);
}
where "i" would be set to 0 at eof.