C++对CSV文件进行读,写,追加操作

1.读取CSV文件

// 读取csv文件
void read_csv(const std::string& file_path) {
  std::cout<<"文件路径: "<< file_path<<"\n";
  std::ifstream csv_data(file_path, std::ios::in);
  std::string line;

  if (!csv_data.is_open()) {
    std::cout << "Error: failed to open file\n";
    std::exit(1);
  }

  std::istringstream sin;  // 将整行字符串读入到字符串流中
  std::vector<std::string> words;
  std::string word;
  std::vector<std::vector<double>> path_points;

  // 读取标题行
  std::getline(csv_data, line);
  // 读取数据
  while (std::getline(csv_data, line)) {
    sin.clear();
    sin.str(line);
    words.clear();
    std::vector<double> path_point;
    while (std::getline(
        sin, word, ',')) {  // 将字符串流sin中的字符读到word中,以字符'逗号'为分隔符
      double value = std::atof(word.c_str());
      path_point.push_back(value);
    }
    path_points.push_back(path_point);
  }

  csv_data.close();  // 关闭文件
}

2.写入CSV文件

void write_csv(const std::string& file_path) {
  std::cout << "写入路径为: " << file_path << "\n";
  std::ofstream out_file(
      file_path,
      std::ios::out);  // 默认通过iso::out方式进行写入,当文件不存在时会进行创建
  if (out_file.is_open()) { //判定文件是否打开
    // 写入标题行
    out_file << "x" << ',' << "y" << ',' << "heading" << ',' << "s" << ','
             << "kappa" << ',' << "flag" << std::endl;

    // 写入10行数据
    for (int i = 0; i < 10; ++i) {
      out_file << std::to_string(i) << ',' << std::to_string(i) << ','
               << std::to_string(i) << ',' << std::to_string(i) << ','
               << std::to_string(i) << ',' << std::to_string(i) << std::endl;
    }

    out_file.close();
  }else{
    std::cout<<"文件无法打开\n";
  }

}

3.向csv文件中追加内容

与第2部分基本相同,只不过是以iso::app方式打开,当文件不存在时会自动创建。

void app_csv(const std::string& file_path) {
  std::ofstream out_file(file_path, std::ios::app);
  if(out_file.is_open()){ //判断文件是否打开
    // 写入10行数据
    for (int i = 10; i < 20; ++i) {
      out_file << std::to_string(i) << ',' << std::to_string(i) << ','
               << std::to_string(i) << ',' << std::to_string(i) << ','
               << std::to_string(i) << ',' << std::to_string(i) << std::endl;
    }
  }else{
    std::cout<<"文件无法打开\n";
  }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++csv文件,可以使用STL库中的fstream和sstream。具体步骤如下: 1. 包含头文件: ```c++ #include <fstream> #include <sstream> #include <iostream> #include <string> #include <vector> ``` 2. 定义读取csv文件的函数: ```c++ // 读取csv文件 std::vector<std::vector<std::string>> readCsv(std::string filename) { std::vector<std::vector<std::string>> data; // 存储csv文件中的数据 std::ifstream file(filename); // 打开csv文件 std::string line; // 存储每行数据的字符串 while (std::getline(file, line)) { // 逐行读取csv文件 std::vector<std::string> row; // 存储每行数据的向量 std::stringstream ss(line); // 将每行数据的字符串转换为流 std::string cell; // 存储每个单元格的字符串 while (std::getline(ss, cell, ',')) { // 逐个单元格读取每行数据 row.push_back(cell); // 将单元格的字符串添加到每行数据的向量中 } data.push_back(row); // 将每行数据的向量添加到存储csv文件数据的向量中 } return data; // 返回存储csv文件数据的向量 } ``` 3. 定义csv文件的函数: ```c++ // csv文件 void writeCsv(std::string filename, std::vector<std::vector<std::string>> data) { std::ofstream file(filename); // 创建csv文件 for (auto row : data) { // 逐行csv文件 for (auto cell : row) { // 逐个单元格入每行数据 file << cell << ','; // 将单元格的字符串csv文件,并添加逗号分隔符 } file << std::endl; // 每行数据入完成后,添加换行符 } file.close(); // 关闭csv文件 } ``` 4. 调用读取csv文件的函数: ```c++ int main() { std::string filename = "data.csv"; // csv文件路径 std::vector<std::vector<std::string>> data = readCsv(filename); // 读取csv文件 writeCsv("new_data.csv", data); // 将读取csv文件数据入新的csv文件 return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值