C/C++读写csv文件(用getline探测逗号分隔符)
2024-10-02 01:07:43
csv文件其实就是文本文件,每行字段用逗号分隔。
代码
- #include <iostream>
- #include <string>
- #include <vector>
- #include <fstream>
- #include <sstream>
- using namespace std;
- int main()
- {
- // 写文件
- ofstream outFile;
- outFile.open("data.csv", ios::out); // 打开模式可省略
- outFile << "name" << ',' << "age" << ',' << "hobby" << endl;
- outFile << "Mike" << ',' << 18 << ',' << "paiting" << endl;
- outFile << "Tom" << ',' << 25 << ',' << "football" << endl;
- outFile << "Jack" << ',' << 21 << ',' << "music" << endl;
- outFile.close();
- // 读文件
- ifstream inFile("data.csv", ios::in);
- string lineStr;
- vector<vector<string>> strArray;
- while (getline(inFile, lineStr))
- {
- // 打印整行字符串
- cout << lineStr << endl;
- // 存成二维表结构
- stringstream ss(lineStr);
- string str;
- vector<string> lineArray;
- // 按照逗号分隔
- while (getline(ss, str, ','))
- lineArray.push_back(str);
- strArray.push_back(lineArray);
- }
- getchar();
- return 0;
- }
结果
http://blog.csdn.net/u012234115/article/details/64465398
最新文章
- iOS 修改UIWebView的UserAgent
- CLR via C#(15)--String,熟悉而又陌生
- oracle和mysql获取当前时间的不同
- u盘在电脑读不出来,但别的可以读,别的u盘在我电脑又可以识别怎么回事?
- C++ 类的动态组件化技术
- leecode 排列的学习
- 常用SNS开源系统比较
- AFNetworiking与ASIHttpRequest对比
- Typings实现智能
- 使用RouteDebugger对MVC路由进行调试
- php多态
- string.PadLeft &; string.PadRight
- ODAC(V9.5.15) 学习笔记(十一)TOraEncryptor、TOraPackage和TOraAlerter
- 使用spring boot +WebSocket实现(后台主动)消息推送
- 《GPU高性能编程CUDA实战》附录四 其他头文件
- this四种绑定方式之间的奇淫技巧
- 别名的使用注意,";";真坑。
- JAVA基础之——数据结构
- CCCallFunc CCCallFuncN CCCallFuncND的区别和使用
- python 之 Collections模块
热门文章
- 前端js实现打印excel表格
- oracle listener.ora的host不能使localhost,而应该是该机器名,否则不能用ip地址进行连接
- css3-3 css3背景样式
- 基于 MySQL 5.6 keepalived的双主搭建
- 删除vector中的重复数据(unique)
- [Angular] Difference between ViewChild and ContentChild
- 【t051】图书管理
- ios获取iphone手机设备型号
- 个人官网第8次升级(新功能、用户体验、修复bug、系统优化)
- Adaptive partitioning scheduler for multiprocessing system