1. CSV-百度百科

2. 代码

  1. #pragma once
  2. //Microsoft Visual Studio 2015 Enterprise
  3. #include<iostream>
  4. #include<fstream>
  5. #include<string>
  6. #include<vector>
  7. #include<cstdio>
  8. #include<cstdlib>
  9. using namespace std;
  10. template<typename base_T>
  11. class modifyCSVfile
  12. {
  13. private:
  14. struct arrInfo
  15. {
  16. double **arrName;
  17. int lineNum;
  18. int rowNum;
  19. };
  20. public:
  21. string saveArray(base_T* arr, int lineNum, int rowNum, string fileName, int precis = 6);
  22. string saveArray(const vector<vector<base_T>>& arr, string fileName, int precis = 6);
  23. vector<vector<double>> CSVtoVector(string fileName);
  24. arrInfo CSVtoArray(string fileName);
  25. int delArray(arrInfo);
  26. };
  27. //*************************************************************************************************************************************************************
  28. //*************************************************************************************************************************************************************
  29. template<typename base_T>
  30. string modifyCSVfile<base_T>::saveArray(base_T* arr, int lineNum, int rowNum, string fileName, int precis)
  31. //函数功能:把一个int/float/double型二维数组,存入CSV文件
  32. //参数1:数组第一个元素地址,e.g.【&array1[0][0]】;参数2:数组行数;参数3:数组列数;参数4:文件名;参数5:设置精度(默认精度是6)
  33. {
  34. fileName += ".csv"; //保存成VSV格式文件,方便用Excel打开
  35. //保存数组到文件。如果文件不存在,创建文件,并写入数据;如果文件存在,清空重新写入
  36. ofstream fout;
  37. fout.open(fileName.c_str(), ios_base::trunc);
  38. fout << showpoint;
  39. fout.precision(precis);
  40. for (int i = 0; i < lineNum; i++)
  41. {
  42. for (int j = 0; j < rowNum; j++)
  43. {
  44. if (j < rowNum - 1)
  45. fout << *(arr + i * rowNum + j) << ","; // arr + i * rowNum + j:找到当前数组元素的顺序索引值
  46. else
  47. fout << *(arr + i * rowNum + j) << endl;
  48. }
  49. }
  50. fout.close();
  51. return fileName;
  52. }
  53. ////用法示例:Visual Studio 2017 Community
  54. //#include<iostream>
  55. //#include<fstream>
  56. //#include<string>
  57. //#include<vector>
  58. //#include"modifyCSVfile.h"
  59. //
  60. //const int arr_lineNum = 5;
  61. //const int arr_rowNum = 7;
  62. //int main()
  63. //{
  64. // //定义一个double型二维数组,并赋值
  65. // double k = 1.1;
  66. // double arr[arr_lineNum][arr_rowNum];
  67. // for (int i = 0; i < arr_lineNum; i++)
  68. // {
  69. // for (int j = 0; j < arr_rowNum; j++)
  70. // {
  71. // arr[i][j] = k;
  72. // k = k + 1;
  73. // }
  74. // }
  75. //
  76. // //输出当前数组到屏幕
  77. // for (int i = 0; i < arr_lineNum; i++)
  78. // {
  79. // for (int j = 0; j < arr_rowNum; j++)
  80. // {
  81. // cout << arr[i][j] << " ";
  82. // }
  83. // cout << endl;
  84. // }
  85. // system("pause");
  86. //
  87. // //把当前数组存如文件。文件位置:当前工程文件夹下。文件格式为.csv,可用文本文档打开,也可用Excel打开。
  88. // modifyCSVfile<double> save;
  89. // save.saveArray(&arr[0][0], arr_lineNum, arr_rowNum, "arr1", 6);
  90. //
  91. // return 0;
  92. //}
  93. //*************************************************************************************************************************************************************
  94. //*************************************************************************************************************************************************************
  95. template<typename base_T>
  96. string modifyCSVfile<base_T>::saveArray(const vector<vector<base_T>>& arr, string fileName, int precis)
  97. //函数功能:把一个vector二维数组,存入CSV文件
  98. //参数1:vector对象名;参数2:文件名;参数3:设置精度(默认精度是6)
  99. {
  100. fileName += ".csv"; //保存成VSV格式文件,方便用Excel打开
  101. //保存数组到文件。如果文件不存在,创建文件,并写入数据;如果文件存在,清空重新写入
  102. ofstream fout;
  103. fout.open(fileName.c_str(), ios_base::trunc);
  104. fout << showpoint;
  105. fout.precision(precis);
  106. for (unsigned int i = 0; i < arr.size(); i++)
  107. {
  108. for (unsigned int j = 0; j < arr[i].size(); j++)
  109. {
  110. if (j < arr[i].size() - 1)
  111. fout << arr[i][j] << ",";
  112. else
  113. fout << arr[i][j] << endl;
  114. }
  115. }
  116. fout.close();
  117. return fileName;
  118. }
  119. ////用法示例:Visual Studio 2017 Community
  120. //#include<iostream>
  121. //#include<fstream>
  122. //#include<string>
  123. //#include<vector>
  124. //#include"modifyCSVfile.h"
  125. //
  126. //int main()
  127. //{
  128. // //定义一个二维vector数组,并赋值
  129. // double k = 1.1;
  130. // int lineNum = 5, rowNum = 7;
  131. // vector<vector<double> > arr2(lineNum, vector<double>(rowNum));
  132. // for (int i = 0; i < lineNum; i++)
  133. // {
  134. // for (int j = 0; j < rowNum; j++)
  135. // {
  136. // arr2[i][j] = k;
  137. // k = k + 1;
  138. // }
  139. // }
  140. //
  141. // //输出当前数组到屏幕
  142. // for (int i = 0; i < lineNum; i++)
  143. // {
  144. // for (int j = 0; j < rowNum; j++)
  145. // cout << arr2[i][j] << " ";
  146. // cout << endl;
  147. // }
  148. // system("pause");
  149. //
  150. // //把当前数组存如文件。文件位置:当前工程文件夹下。文件格式为.csv,可用文本文档打开,也可用Excel打开。
  151. // modifyCSVfile<double> save;
  152. // save.saveArray(arr2, "arr2", 6);
  153. //
  154. // return 0;
  155. //}
  156. //*************************************************************************************************************************************************************
  157. //*************************************************************************************************************************************************************
  158. template<typename base_T>
  159. vector<vector<double>> modifyCSVfile<base_T>::CSVtoVector(string fileName)
  160. //函数功能:读取只包含数据不包含文字的CSV文件,并取出里边的数据存入到二维double型vector数组中
  161. //参数1:文件名
  162. {
  163. fileName += ".csv";
  164. ifstream fin;
  165. fin.open(fileName.c_str(), ios_base::in); //以只读的方式打开文件
  166. //跳过CSV文件开头可能出现的空行
  167. char ch;
  168. while (fin.get(ch))
  169. {
  170. if (ch == '\n')
  171. continue;
  172. else
  173. break;
  174. }
  175. streamoff pos = fin.tellg(); //保存当前位置
  176. fin.seekg(pos - 1, ios_base::beg); //返回到文件中有数据开始的那一行的首位
  177. //获取CSV文件中数据的行数
  178. int lineNum = 0, rowNum = 0;
  179. string buf;
  180. while (getline(fin, buf) && !buf.empty())
  181. {
  182. lineNum = lineNum + 1;
  183. }
  184. fin.clear(); //getline()读取到文件尾,接下来输入流被阻断。需要重置输入流,如果不重置,接下来将无法获取文件数据。
  185. //获取CSV文件中数据的列数
  186. fin.seekg(pos - 1, ios_base::beg); //返回到文件中有数据开始的那一行的首位
  187. while (fin.get(ch))
  188. {
  189. if (ch == ',')
  190. {
  191. rowNum = rowNum + 1;
  192. }
  193. else
  194. if (ch == '\n')
  195. break;
  196. }
  197. rowNum = rowNum + 1;
  198. //把CSV文件中的数据存入double型的vector中
  199. fin.seekg(pos - 1, ios_base::beg); //返回到文件中有数据开始的那一行的首位
  200. buf.erase(0);
  201. double temp;
  202. vector<vector<double>>vect(lineNum, vector<double>(rowNum));
  203. for (int i = 0; i < lineNum; i++)
  204. {
  205. for (int j = 0; j < rowNum; j++)
  206. {
  207. while (fin.get(ch))
  208. {
  209. if (ch != ',' && ch != '\n')
  210. buf += ch;
  211. else
  212. {
  213. temp = atof(buf.c_str());
  214. vect[i][j] = temp;
  215. buf.erase(0);
  216. break;
  217. }
  218. }
  219. }
  220. }
  221. fin.close();
  222. return vect;
  223. }
  224. ////用法示例:Visual Studio 2017 Community
  225. //#include<iostream>
  226. //#include<fstream>
  227. //#include<string>
  228. //#include<vector>
  229. //#include<cstdio>
  230. //#include<cstdlib>
  231. //#include"modifyCSVfile.h"
  232. //
  233. //using namespace std;
  234. //
  235. //int main()
  236. //{
  237. //
  238. // vector<vector<double>> arr1; //创建一个二维vector数组,用于接受函数调用返回的二维vector数组
  239. // modifyCSVfile<double> read;
  240. // arr1 = read.CSVtoVector("arr2");
  241. //
  242. // //输出arr1到屏幕
  243. // for (int i = 0; i < 4; i++)
  244. // {
  245. // for (int j = 0; j < 4; j++)
  246. // {
  247. // cout << arr1[i][j] << " ";
  248. // }
  249. // cout << endl;
  250. // }
  251. //
  252. // system("pause");
  253. // return 0;
  254. //}
  255. //*************************************************************************************************************************************************************
  256. //*************************************************************************************************************************************************************
  257. template<typename base_T>
  258. typename modifyCSVfile<base_T>::arrInfo modifyCSVfile<base_T>::CSVtoArray(string fileName)
  259. //函数功能:读取只包含数据不包含文字的CSV文件,并取出里边的数据存入到new创建的动态二维数组中
  260. //参数1:文件名
  261. {
  262. fileName += ".csv";
  263. ifstream fin;
  264. fin.open(fileName.c_str(), ios_base::in); //以只读的方式打开文件
  265. //跳过CSV文件开头可能出现的空行
  266. char ch;
  267. while (fin.get(ch))
  268. {
  269. if (ch == '\n')
  270. continue;
  271. else
  272. break;
  273. }
  274. streamoff pos = fin.tellg(); //保存当前位置
  275. fin.seekg(pos - 1, ios_base::beg); //返回到文件中有数据开始的那一行的首位
  276. //获取CSV文件中数据的行数
  277. int lineNum = 0, rowNum = 0;
  278. string buf;
  279. while (getline(fin, buf) && !buf.empty())
  280. {
  281. lineNum = lineNum + 1;
  282. }
  283. fin.clear(); //getline()读取到文件尾,接下来输入流被阻断。需要重置输入流,如果不重置,接下来将无法获取文件数据。
  284. //获取CSV文件中数据的列数
  285. fin.seekg(pos - 1, ios_base::beg); //返回到文件中有数据开始的那一行的首位
  286. while (fin.get(ch))
  287. {
  288. if (ch == ',')
  289. {
  290. rowNum = rowNum + 1;
  291. }
  292. else
  293. if (ch == '\n')
  294. break;
  295. }
  296. rowNum = rowNum + 1;
  297. //new创建二维动态数组
  298. double **arr = new double *[lineNum];
  299. for (int i = 0; i < lineNum; i++)
  300. {
  301. arr[i] = new double[rowNum];
  302. }
  303. //把CSV文件中的数据存入二维数组中
  304. fin.seekg(pos - 1, ios_base::beg); //返回到文件中有数据开始的那一行的首位
  305. buf.erase(0);
  306. double temp;
  307. for (int i = 0; i < lineNum; i++)
  308. {
  309. for (int j = 0; j < rowNum; j++)
  310. {
  311. while (fin.get(ch))
  312. {
  313. if (ch != ',' && ch != '\n')
  314. buf += ch;
  315. else
  316. {
  317. temp = atof(buf.c_str());
  318. arr[i][j] = temp;
  319. buf.erase(0);
  320. break;
  321. }
  322. }
  323. }
  324. }
  325. fin.close();
  326. //创建一个结构对象,保存:指向当前数组的指针、当前数组行数、当前数组列数
  327. arrInfo info;
  328. info.arrName = arr;
  329. info.lineNum = lineNum;
  330. info.rowNum = rowNum;
  331. return info; //返回结构
  332. }
  333. ////用法示例:Visual Studio 2017 Community
  334. //#include<iostream>
  335. //#include<fstream>
  336. //#include<string>
  337. //#include<vector>
  338. //#include<cstdio>
  339. //#include<cstdlib>
  340. //#include"modifyCSVfile.h"
  341. //
  342. //using namespace std;
  343. //
  344. //int main()
  345. //{
  346. // //读取CSV文件arr2中的数据,返回一个结构。结构包括:指向数组的指针、数组行数、数组列数
  347. // modifyCSVfile<double> read;
  348. // auto arr1 = read.CSVtoArray("arr2");
  349. //
  350. // //输出数组数据到屏幕
  351. // for (int i = 0; i < arr1.lineNum; i++)
  352. // {
  353. // for (int j = 0; j < arr1.rowNum; j++)
  354. // {
  355. // cout << arr1.arrName[i][j] << " ";
  356. // }
  357. // cout << endl;
  358. // }
  359. //
  360. // //注意:由于调用CSVtoArray函数时,生成的数组是用new分配的,所以数据用完之后要记得释放。
  361. // //这里没有释放,可以调用delArray函数释放。
  362. //
  363. // system("pause");
  364. // return 0;
  365. //}
  366. //*************************************************************************************************************************************************************
  367. //*************************************************************************************************************************************************************
  368. template<typename base_T>
  369. int modifyCSVfile<base_T>::delArray(arrInfo info)
  370. //函数功能:删除调用CSVtoArray函数时生成的动态二维数组
  371. //由于CSVtoArray函数里边的二维数组是使用new创建的,所以用完之后要释放
  372. {
  373. for (int i = 0; i < info.lineNum; i++)
  374. delete[] info.arrName[i];
  375. delete[] info.arrName;
  376. return 0;
  377. }
  378. ////用法示例:Visual Studio 2017 Community
  379. //#include<iostream>
  380. //#include<fstream>
  381. //#include<string>
  382. //#include<vector>
  383. //#include<cstdio>
  384. //#include<cstdlib>
  385. //#include"modifyCSVfile.h"
  386. //
  387. //using namespace std;
  388. //
  389. //int main()
  390. //{
  391. // //读取CSV文件arr2中的数据,返回一个结构。结构包括:指向数组的指针、数组行数、数组列数
  392. // modifyCSVfile<double> read;
  393. // auto arr1 = read.CSVtoArray("arr2");
  394. //
  395. // //输出数组数据到屏幕
  396. // for (int i = 0; i < arr1.lineNum; i++)
  397. // {
  398. // for (int j = 0; j < arr1.rowNum; j++)
  399. // {
  400. // cout << arr1.arrName[i][j] << " ";
  401. // }
  402. // cout << endl;
  403. // }
  404. //
  405. // //释放调用CSVtoArray函数时用new生成的动态二维数组
  406. // modifyCSVfile<double> del;
  407. // del.delArray(arr1);
  408. //
  409. // system("pause");
  410. // return 0;
  411. //}


未完 ......

点击访问原文(进入后根据右侧标签,快速定位到本文)

C++ 把数组数据存入 CSV 文件,以及读取 CSV 文件的数据的更多相关文章

  1. 从文件中读取yuv和h264数据

    1.从文件中读取h264数据 参考ffmpeg avc.c写的从文件中一帧帧读取h.264数据的demo #include <stdio.h> #include <stdlib.h& ...

  2. spring 框架的xml文件如何读取properties文件数据

    spring 框架的xml文件如何读取properties文件数据 第一步:在spring配置文件中 注意:value可以多配置几个properties文件 <bean id="pro ...

  3. c文件二进制读取写入文件、c语言实现二进制(01)转化成txt格式文本、c读取文件名可变

    c语言实现二进制(01)转化成txt格式文本: 下面的程序只能实现ascall对应字符转换,如果文件内出现中文字符,则会出现错误. 本程序要自己创建个文本格式的输入文件a1.txt,编译后能将文本文件 ...

  4. java读取 500M 以上文件,java读取大文件

    java 读取txt,java读取大文件 设置缓存大小BUFFER_SIZE ,Config.tempdatafile是文件地址 来源博客http://yijianfengvip.blog.163.c ...

  5. 通过文件路径读取CSV表格内的数据

    ReadDataFromCSV.h UCLASS() class MYPROJECT_API UReadDataFromCSV : public UBlueprintFunctionLibrary { ...

  6. C#写入(覆盖形式)数据到CSV文件 和 读取CSV文件

    /// <summary> /// 写入数据到CSV文件,覆盖形式 /// </summary> /// <param name="csvPath"& ...

  7. php操作文件(读取写入文件)

    一,PHP如何读取文件 PHP读取文件可以读取当前服务器或远程服务器中的文件.其步骤是:打开文件.读文件和关闭文件. 1,PHP如何打开文件 使用PHP函数fopen()打开一个文件,fopen()一 ...

  8. Node.js——fs模块(文件系统),创建、删除目录(文件),读取写入文件流

    /* 1. fs.stat 检测是文件还是目录(目录 文件是否存在) 2. fs.mkdir 创建目录 (创建之前先判断是否存在) 3. fs.writeFile 写入文件(文件不存在就创建,但不能创 ...

  9. [Python] python3 文件操作:从键盘输入、打开关闭文件、读取写入文件、重命名与删除文件等

    1.从键盘输入 Python 2有两个内置的函数用于从标准输入读取数据,默认情况下来自键盘.这两个函数分别是:input()和raw_input(). Python 3中,不建议使用raw_input ...

  10. 在JavaScript文件中读取properties文件的方法

    假设有JavaScript文件叫做:readproperties.js,这个文件需要读取config.properties这个配置文件,步骤如下: 1.  下载插件jquery.i18n.proper ...

随机推荐

  1. codeforces1276A As Simple as One and Two

    C.As Simple as One and Two A. As Simple as One and Two time limit per test 3 seconds memory limit pe ...

  2. BZOJ 5469: [FJOI2018]领导集团问题 dp+线段树合并

    在 dp 问题中,如果发现可以用后缀最大值来进行转移的话可以考虑去查分这个后缀最大值. 这样的话可以用差分的方式来方便地进行维护 ~ #include <bits/stdc++.h> #d ...

  3. Vue之路由

    1. SPA是什么 单页Web应用(single page application,SPA),就是只有一个Web页面的应用, 是加载单个HTML页面,并在用户与应用程序交互时动态更新该页面的Web应用 ...

  4. CSPS_110

    永远不要相信出题人诸如“保证图联通”之类的鬼话. T1 最优情况一定为从LR最高的不同位以下全是1 T2 折半搜索 T3 1.我算法不是mlog^2m,最坏情况下mlogm再乘个根号m, 考试的时候没 ...

  5. 安卓入门教程(十五)- Fragment,Service,WAMP下载

    Fragment概述 Fragment可以被嵌入到Activity中,一个Activity可以有多个Fragment. 创建Fragment public class MyFragment exten ...

  6. c博客作业-我的第一篇博客

    1.你对网络专业或者计算机专业了解是怎样的? 以前接触计算机,只是把它当作娱乐的工具,并没有太过了解,现在我通过查阅了解了一些计算机的知识. 计算机专业的学生要学习的不仅是会使用,而且要学习计算机的基 ...

  7. URL的作用是什么?它由几部分组成?

    URL是统一资源定位符,对可以从互联网上得到的资源的位置和访问方法的一种简洁的表示,是互联网上标准资源的地址.互联网上的每个文件都有一个唯一的URL,它包含的信息指出文件的位置以及浏览器应该怎么处理它 ...

  8. 【POJ2996】Help Me with the Game

    题目传送门 本题知识点:模拟(如果对国际象棋不熟悉的同学可以先百度一下) 题意很简单,就是让我们找出白棋跟黑棋每枚棋子的位置,并要按照一定的顺序输出( K -> Q -> R -> ...

  9. hive集成kerberos

    1.票据的生成 kdc服务器操作,生成用于hive身份验证的principal 1.1.创建principal # kadmin.local -q “addprinc -randkey hive/yj ...

  10. AT1879 2 つの山札

    题面 题解 直接求解比较麻烦,考虑将问题进行转化. 设序列\(a = \{3, 1, 4, 2, 5\}, b = \{3, 2, 4, 1, 5\}\),那么我们构造一个正方形方格,将\(a\)放在 ...