c ++程序读取excel数据的单列,其中包含两个由逗号分隔的double值,并将每个值存储在自己的数组中

我有一个excel文件,其中有一个双列值的单个列,一个逗号,然后在每个单元格中的另一个double值。
例如:5.393,4.1245

我希望能够读取这些值并将它们存储在单独的数组中。 一个包含x值的数组和一个包含y值的数组,以便我可以用它们做其他事情。

// string to be read from excel string s1; // declare object ifstream input; // open workbook input.open("Workbook3.csv"); // check if file exists if(input.fail()) { 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; 

我遇到的麻烦是如何读取双精度值,直到达到逗号分隔符,然后在分隔符后读取另一个值。 之后,也告诉它移动到下一行,直到它最终到达一个空的单元格。 也许这是使用VBS更好的主意? 任何帮助是极大的赞赏。

假设文件只是一个文本文件,格式为double,double \ n,您可以使用下面的代码将其读入两个数组:

  std::vector<double> xs; std::vector<double> ys; while(getline(input,s1)) { std::istringstream ss(s1); double x, y; ss >> x; // read past the comma ss.get(); ss >> y; //add to the arrays xs.push_back(x); ys.push_back(y); } 

这处理读取逗号和行尾的getline。 不提供delim参数假定读到行尾。

然后创build一个stringstream,将string视为一个文件,并允许您像文件stream一样parsingstring。 它不是最快的方式,但它的简单,并以简单的方式处理文件和行结束。