c++ 存取csv数据

保存

 calibration_data.open("threshold.csv", std::ios::out | std::ios::trunc);
    // calibration_data << "min_threshold" << std::endl;
    for (int h = 0; h < threshold_data.size(); h++) {
      // threshold_data[h] = threshold_data[h] ;
      calibration_data << threshold_data[h] << ",";
    }
    calibration_data << std::endl;
    calibration_data.close();

csv数据以’,'分隔列,以std::endl分隔行

读取

 std::ifstream inFile("threshold.csv", std::ios::in);
    std::string value;
    std::vector<double> data;
    getline(inFile, value);  // 读取整行进value中 读取第一行
    std::stringstream ss(value);
    std::string str;
    while (getline(ss, str, ',')) {        // 以逗号为分隔读取string
      data.push_back(atof(str.c_str()));   // string转为double
    }
    std::cout << "data" << std::endl;
    for (auto a : data) {
      std::cout << a << " ";
    }
    std::cout << std::endl;
    threshold_data = data;
©️2020 CSDN 皮肤主题: 游动-白 设计师:上身试试 返回首页