csv文件其实就是文本文件,每行字段用逗号分隔。

代码

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <fstream>
  5. #include <sstream>
  6. using namespace std;
  7. int main()
  8. {
  9. // 写文件
  10. ofstream outFile;
  11. outFile.open("data.csv", ios::out); // 打开模式可省略
  12. outFile << "name" << ',' << "age" << ',' << "hobby" << endl;
  13. outFile << "Mike" << ',' << 18 << ',' << "paiting" << endl;
  14. outFile << "Tom" << ',' << 25 << ',' << "football" << endl;
  15. outFile << "Jack" << ',' << 21 << ',' << "music" << endl;
  16. outFile.close();
  17. // 读文件
  18. ifstream inFile("data.csv", ios::in);
  19. string lineStr;
  20. vector<vector<string>> strArray;
  21. while (getline(inFile, lineStr))
  22. {
  23. // 打印整行字符串
  24. cout << lineStr << endl;
  25. // 存成二维表结构
  26. stringstream ss(lineStr);
  27. string str;
  28. vector<string> lineArray;
  29. // 按照逗号分隔
  30. while (getline(ss, str, ','))
  31. lineArray.push_back(str);
  32. strArray.push_back(lineArray);
  33. }
  34. getchar();
  35. return 0;
  36. }
 

结果

 
 
http://blog.csdn.net/u012234115/article/details/64465398

最新文章

  1. iOS 修改UIWebView的UserAgent
  2. CLR via C#(15)--String,熟悉而又陌生
  3. oracle和mysql获取当前时间的不同
  4. u盘在电脑读不出来,但别的可以读,别的u盘在我电脑又可以识别怎么回事?
  5. C++ 类的动态组件化技术
  6. leecode 排列的学习
  7. 常用SNS开源系统比较
  8. AFNetworiking与ASIHttpRequest对比
  9. Typings实现智能
  10. 使用RouteDebugger对MVC路由进行调试
  11. php多态
  12. string.PadLeft &amp; string.PadRight
  13. ODAC(V9.5.15) 学习笔记(十一)TOraEncryptor、TOraPackage和TOraAlerter
  14. 使用spring boot +WebSocket实现(后台主动)消息推送
  15. 《GPU高性能编程CUDA实战》附录四 其他头文件
  16. this四种绑定方式之间的奇淫技巧
  17. 别名的使用注意,&quot;&quot;真坑。
  18. JAVA基础之——数据结构
  19. CCCallFunc CCCallFuncN CCCallFuncND的区别和使用
  20. python 之 Collections模块

热门文章

  1. 前端js实现打印excel表格
  2. oracle listener.ora的host不能使localhost,而应该是该机器名,否则不能用ip地址进行连接
  3. css3-3 css3背景样式
  4. 基于 MySQL 5.6 keepalived的双主搭建
  5. 删除vector中的重复数据(unique)
  6. [Angular] Difference between ViewChild and ContentChild
  7. 【t051】图书管理
  8. ios获取iphone手机设备型号
  9. 个人官网第8次升级(新功能、用户体验、修复bug、系统优化)
  10. Adaptive partitioning scheduler for multiprocessing system