写
void Spline_process::csv_save(Eigen::MatrixXd &res){
ofstream dataFile;
dataFile.open("sp.csv", ios::out | ios::trunc);
for (int i = 0; i < res.rows(); i++)
{
for (int j = 0; j < res.cols(); j++)
{
dataFile << res(i,j) << ","; // 写入数据
// cout << res(i,j) << ",";
}
dataFile << endl; // 换行
// cout << endl;
}
dataFile.close(); // 关闭文档
}
读
std::ifstream f;
f.open("install/app_controller/lib/app_controller/test.csv"); /* open file with filename as argument */
if (!f.is_open()) { /* validate file open for reading */
std::cerr << "error: file open failed " << ".\n";
return ;
}
std::string line, val; /* string for line & value */
std::vector<std::vector<double>> array; /* vector of vector<int> */
while (std::getline (f, line)) { /* read each line */
std::vector<double> vv; /* row vector v */
std::stringstream s (line); /* stringstream line */
while (getline (s, val, ',')) /* get each value (',' delimited) */
vv.push_back (std::stod (val)); /* add to row vector */
array.push_back (vv); /* add row vector to array */
}