mDotL mDotL - 1 month ago 14
C++ Question

c++ program to read single column of excel data containing two double values separated by a comma and store each in their own arrays

I have an excel file in which there is a single column with one double value, a comma, and then another double value in each cell.

ex: 5.393,4.1245

I would like to be able to read in those values and store them in separate arrays. One array with x values and one array with y values so that I can do other things with them.

// string to be read from excel
string s1;

// declare object
ifstream input;

// open workbook"Workbook3.csv");

// check if file exists
cout << "File does not exist." << endl;
cout << "Exiting Program." << endl;
return 0;

while(getline(input, s1, ','))
// using this format I would need to convert string to double perhaps and then read into array?

return 0;

The trouble I'm having is figuring out how to read in a double value until a comma delimiter is reached and then reading in another value after the delimiter. Following that, also telling it to move on to the next row until it would eventually reach an empty cell. Perhaps it would be a better idea to use VBS for this?
Any help is greatly appreciated.


Assuming the file is simply a text file in the format of double,double\n you could use the following code to read it into two arrays:

    std::vector<double> xs;
std::vector<double> ys;

    std::istringstream ss(s1);
    double x, y;

    ss >> x;
    // read past the comma

    ss >> y;

    //add to the arrays

This handles reading the comma and end of line with getline. Providing no delim argument assumes read to end of line.

Then create a string stream that treats a string somewhat like a file and allows you to parse the string like a file stream. Its not the fastest way to do this, but its simple and handles end of file and end of line in a simple way.