C++对CSV文件进行读,写,追加操作_c++ 可以读取csv文件吗-程序员宅基地

技术标签: c++  文件读写操作  开发语言  

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";
  }
}

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/xtxu_16/article/details/134587886

智能推荐

LA 2038 Strategic game 树形DP ._la 2038战略游戏-程序员宅基地

文章浏览阅读243次。题目地址:http://vjudge.net/problem/UVALive-2038 以前做过类似的题 就是,无根转有根 然后普通的DP d[u][i] 表示u节点的父亲有没有被选中,i==1表示被选中,反之没有 为什么弄u的父节点呢,因为 如果弄u的节点信息,那么当u没被选中,子结点至少有一个要被选中,那么只能枚举被选中的那个,无法递推,有后效性#include

leaflet基本使用_leaflet中文文档-程序员宅基地

文章浏览阅读3.5k次,点赞3次,收藏10次。官网(英文):https://iclient.supermap.io/examples/leaflet/examples.html#iServer。高德官网的名词解释:https://lbs.amap.com/api/javascript-api/guide/abc/components。项目地址 gitee:https://gitee.com/philippines-kisses-snow/leaflet-map。(1)直接添加,官网当中是直接建立一个标记,并添加到地图(不推荐,不好管理)_leaflet中文文档

解决在VS2019中创建不了网站_visual studio无法新建空白网站-程序员宅基地

文章浏览阅读611次。VS2019无法新建网站,怎么办?_visual studio无法新建空白网站

ROS 机器人操作系统:Ubuntu 安装 ROS Noetic_ubuntu系统安装ros noetic包-程序员宅基地

文章浏览阅读3k次,点赞6次,收藏43次。本文介绍如何在 Ubuntu 20.04 安装 ROS Noetic 软件包,并进行简单的测试。安装步骤1、配置 Ubuntu 软件仓库配置 Ubuntu 软件仓库支持 “restricted” “universe” 和 “multiverse” 来源,最简单的方法是在「软件和更新」配置窗口进行勾选,如下图所示。扩展阅读:Ubuntu 软件源、Ubuntu Repositories2、设置 sources.list执行下面命令,设置从官方源(packages.ros.org)下载 ROS 软件_ubuntu系统安装ros noetic包

Error:too many padding sections on bottom border.-程序员宅基地

文章浏览阅读8.5k次。异常信息:Error:too many padding sections on bottom border.原因:使用andoridstudio制作.9图错误。 解决只怪我把线画多了。修改后的.9图片 这样问题就解决了..._too many padding sections on bottom border

Django-Redis-程序员宅基地

文章浏览阅读554次,点赞10次,收藏10次。NoSQL:(不支持sql语句)RedisMongoDBkey-value数据库(非关系性数据库)

随便推点

Linux下九个实用脚本_linux脚本-程序员宅基地

文章浏览阅读1.9k次,点赞3次,收藏6次。root@ansible ~]# watch -n 1 sh sd.sh 加上watch -n 1 来判断网卡实时流量。if [ "$RT" -gt 250 ] 限定次数250 超过就屏蔽。echo "网站$URL坏掉,请及时处理"echo "网站高危$URL"echo "磁盘已经沾满不能存储数据!echo "$name 创建成功"echo "$name 创建成功"echo "等待磁盘IO响应使用率: $LL"不过要退出 watch 才会使IP屏蔽。_linux脚本

【Bug解决】ImportError: C extension: No module named ‘pandas._libs.tslibs.base‘ not built._importerror: c extension: none not built. if you w-程序员宅基地

文章浏览阅读4.3k次,点赞6次,收藏11次。问题描述笔者用Pyinstaller打包成exe文件,在其他电脑端运行时出现如下问题ImportError: C extension: No module named 'pandas._libs.tslibs.base' not built. If you want to import pandas from the source directory, you may need to run 'python setup.py build_ext --force' to build the C exten_importerror: c extension: none not built. if you want to import pandas from

tf.tile,tf.gather,t f.concat,t f .expand_dims,tf.add_n, tf.stack,tf.sparse_to_dense_tf add_n concat-程序员宅基地

文章浏览阅读345次。temp = tf.range(0,10)*10 + tf.constant(1,shape=[10])temp2 = tf.gather(temp,[1,5,9])with tf.Session() as sess: print (sess.run(temp)) print (sess.run(temp2))输出结果[ 1 11 21 31 41 51 61 ..._tf add_n concat

用linux脚本插入10w级的数据数据库为mysql-程序员宅基地

文章浏览阅读177次。插入大量数据到MySQL数据库可以使用以下步骤:准备数据:你需要一个数据文件,包含需要插入的所有数据。每行都是一条记录,字段之间使用适当的分隔符分开。创建数据库:如果没有相应的数据库,请先创建一个。创建表:创建一个表来存储数据。导入数据:使用MySQL的"LOAD DATA INFILE"命令导入数据。该命令可以从文件中快速加载大量数据到MySQL表中。以下是使用bash脚本..._linux sql插入大量数据

腾讯云域名免费SSL证书怎么获取?HTTPS免费配置_腾讯 域名证书-程序员宅基地

文章浏览阅读316次,点赞5次,收藏4次。总之,通过腾讯云免费SSL证书申请教程,您可以轻松为自己的域名获取免费SSL证书,并实现HTTPS的安全配置。同时,结合腾讯云的服务器购买优惠政策,您还可以以更经济的成本搭建起安全、高效的网站环境。腾讯云将为您的域名生成免费的SSL证书,并通过您提供的邮箱发送相关通知。这样,您就成功获取了免费的SSL证书,为您的网站加上了一把安全锁。那么,如何在腾讯云上为自己的域名免费获取SSL证书,并实现HTTPS的安全配置呢?此外,如果您在配置SSL证书的过程中需要购买服务器,不妨关注腾讯云的促销活动。_腾讯 域名证书

R语言ggplot2包绘制双坐标轴图(双Y轴图)实战:两个Y轴分别使用不同的刻度范围_ggplot双侧轴,两侧轴起始高度不一样,数值刻度也不一样-程序员宅基地

文章浏览阅读146次。R语言ggplot2包绘制双坐标轴图(双Y轴图)实战:两个Y轴分别使用不同的刻度范围_ggplot双侧轴,两侧轴起始高度不一样,数值刻度也不一样

推荐文章

热门文章

相关标签