C++ 把数组数据存入 CSV 文件,以及读取 CSV 文件的数据
1. CSV-百度百科
2. 代码
#pragma once
//Microsoft Visual Studio 2015 Enterprise
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<cstdio>
#include<cstdlib>
using namespace std;
template<typename base_T>
class modifyCSVfile
{
private:
struct arrInfo
{
double **arrName;
int lineNum;
int rowNum;
};
public:
string saveArray(base_T* arr, int lineNum, int rowNum, string fileName, int precis = 6);
string saveArray(const vector<vector<base_T>>& arr, string fileName, int precis = 6);
vector<vector<double>> CSVtoVector(string fileName);
arrInfo CSVtoArray(string fileName);
int delArray(arrInfo);
};
//*************************************************************************************************************************************************************
//*************************************************************************************************************************************************************
template<typename base_T>
string modifyCSVfile<base_T>::saveArray(base_T* arr, int lineNum, int rowNum, string fileName, int precis)
//函数功能:把一个int/float/double型二维数组,存入CSV文件
//参数1:数组第一个元素地址,e.g.【&array1[0][0]】;参数2:数组行数;参数3:数组列数;参数4:文件名;参数5:设置精度(默认精度是6)
{
fileName += ".csv"; //保存成VSV格式文件,方便用Excel打开
//保存数组到文件。如果文件不存在,创建文件,并写入数据;如果文件存在,清空重新写入
ofstream fout;
fout.open(fileName.c_str(), ios_base::trunc);
fout << showpoint;
fout.precision(precis);
for (int i = 0; i < lineNum; i++)
{
for (int j = 0; j < rowNum; j++)
{
if (j < rowNum - 1)
fout << *(arr + i * rowNum + j) << ","; // arr + i * rowNum + j:找到当前数组元素的顺序索引值
else
fout << *(arr + i * rowNum + j) << endl;
}
}
fout.close();
return fileName;
}
////用法示例:Visual Studio 2017 Community
//#include<iostream>
//#include<fstream>
//#include<string>
//#include<vector>
//#include"modifyCSVfile.h"
//
//const int arr_lineNum = 5;
//const int arr_rowNum = 7;
//int main()
//{
// //定义一个double型二维数组,并赋值
// double k = 1.1;
// double arr[arr_lineNum][arr_rowNum];
// for (int i = 0; i < arr_lineNum; i++)
// {
// for (int j = 0; j < arr_rowNum; j++)
// {
// arr[i][j] = k;
// k = k + 1;
// }
// }
//
// //输出当前数组到屏幕
// for (int i = 0; i < arr_lineNum; i++)
// {
// for (int j = 0; j < arr_rowNum; j++)
// {
// cout << arr[i][j] << " ";
// }
// cout << endl;
// }
// system("pause");
//
// //把当前数组存如文件。文件位置:当前工程文件夹下。文件格式为.csv,可用文本文档打开,也可用Excel打开。
// modifyCSVfile<double> save;
// save.saveArray(&arr[0][0], arr_lineNum, arr_rowNum, "arr1", 6);
//
// return 0;
//}
//*************************************************************************************************************************************************************
//*************************************************************************************************************************************************************
template<typename base_T>
string modifyCSVfile<base_T>::saveArray(const vector<vector<base_T>>& arr, string fileName, int precis)
//函数功能:把一个vector二维数组,存入CSV文件
//参数1:vector对象名;参数2:文件名;参数3:设置精度(默认精度是6)
{
fileName += ".csv"; //保存成VSV格式文件,方便用Excel打开
//保存数组到文件。如果文件不存在,创建文件,并写入数据;如果文件存在,清空重新写入
ofstream fout;
fout.open(fileName.c_str(), ios_base::trunc);
fout << showpoint;
fout.precision(precis);
for (unsigned int i = 0; i < arr.size(); i++)
{
for (unsigned int j = 0; j < arr[i].size(); j++)
{
if (j < arr[i].size() - 1)
fout << arr[i][j] << ",";
else
fout << arr[i][j] << endl;
}
}
fout.close();
return fileName;
}
////用法示例:Visual Studio 2017 Community
//#include<iostream>
//#include<fstream>
//#include<string>
//#include<vector>
//#include"modifyCSVfile.h"
//
//int main()
//{
// //定义一个二维vector数组,并赋值
// double k = 1.1;
// int lineNum = 5, rowNum = 7;
// vector<vector<double> > arr2(lineNum, vector<double>(rowNum));
// for (int i = 0; i < lineNum; i++)
// {
// for (int j = 0; j < rowNum; j++)
// {
// arr2[i][j] = k;
// k = k + 1;
// }
// }
//
// //输出当前数组到屏幕
// for (int i = 0; i < lineNum; i++)
// {
// for (int j = 0; j < rowNum; j++)
// cout << arr2[i][j] << " ";
// cout << endl;
// }
// system("pause");
//
// //把当前数组存如文件。文件位置:当前工程文件夹下。文件格式为.csv,可用文本文档打开,也可用Excel打开。
// modifyCSVfile<double> save;
// save.saveArray(arr2, "arr2", 6);
//
// return 0;
//}
//*************************************************************************************************************************************************************
//*************************************************************************************************************************************************************
template<typename base_T>
vector<vector<double>> modifyCSVfile<base_T>::CSVtoVector(string fileName)
//函数功能:读取只包含数据不包含文字的CSV文件,并取出里边的数据存入到二维double型vector数组中
//参数1:文件名
{
fileName += ".csv";
ifstream fin;
fin.open(fileName.c_str(), ios_base::in); //以只读的方式打开文件
//跳过CSV文件开头可能出现的空行
char ch;
while (fin.get(ch))
{
if (ch == '\n')
continue;
else
break;
}
streamoff pos = fin.tellg(); //保存当前位置
fin.seekg(pos - 1, ios_base::beg); //返回到文件中有数据开始的那一行的首位
//获取CSV文件中数据的行数
int lineNum = 0, rowNum = 0;
string buf;
while (getline(fin, buf) && !buf.empty())
{
lineNum = lineNum + 1;
}
fin.clear(); //getline()读取到文件尾,接下来输入流被阻断。需要重置输入流,如果不重置,接下来将无法获取文件数据。
//获取CSV文件中数据的列数
fin.seekg(pos - 1, ios_base::beg); //返回到文件中有数据开始的那一行的首位
while (fin.get(ch))
{
if (ch == ',')
{
rowNum = rowNum + 1;
}
else
if (ch == '\n')
break;
}
rowNum = rowNum + 1;
//把CSV文件中的数据存入double型的vector中
fin.seekg(pos - 1, ios_base::beg); //返回到文件中有数据开始的那一行的首位
buf.erase(0);
double temp;
vector<vector<double>>vect(lineNum, vector<double>(rowNum));
for (int i = 0; i < lineNum; i++)
{
for (int j = 0; j < rowNum; j++)
{
while (fin.get(ch))
{
if (ch != ',' && ch != '\n')
buf += ch;
else
{
temp = atof(buf.c_str());
vect[i][j] = temp;
buf.erase(0);
break;
}
}
}
}
fin.close();
return vect;
}
////用法示例:Visual Studio 2017 Community
//#include<iostream>
//#include<fstream>
//#include<string>
//#include<vector>
//#include<cstdio>
//#include<cstdlib>
//#include"modifyCSVfile.h"
//
//using namespace std;
//
//int main()
//{
//
// vector<vector<double>> arr1; //创建一个二维vector数组,用于接受函数调用返回的二维vector数组
// modifyCSVfile<double> read;
// arr1 = read.CSVtoVector("arr2");
//
// //输出arr1到屏幕
// for (int i = 0; i < 4; i++)
// {
// for (int j = 0; j < 4; j++)
// {
// cout << arr1[i][j] << " ";
// }
// cout << endl;
// }
//
// system("pause");
// return 0;
//}
//*************************************************************************************************************************************************************
//*************************************************************************************************************************************************************
template<typename base_T>
typename modifyCSVfile<base_T>::arrInfo modifyCSVfile<base_T>::CSVtoArray(string fileName)
//函数功能:读取只包含数据不包含文字的CSV文件,并取出里边的数据存入到new创建的动态二维数组中
//参数1:文件名
{
fileName += ".csv";
ifstream fin;
fin.open(fileName.c_str(), ios_base::in); //以只读的方式打开文件
//跳过CSV文件开头可能出现的空行
char ch;
while (fin.get(ch))
{
if (ch == '\n')
continue;
else
break;
}
streamoff pos = fin.tellg(); //保存当前位置
fin.seekg(pos - 1, ios_base::beg); //返回到文件中有数据开始的那一行的首位
//获取CSV文件中数据的行数
int lineNum = 0, rowNum = 0;
string buf;
while (getline(fin, buf) && !buf.empty())
{
lineNum = lineNum + 1;
}
fin.clear(); //getline()读取到文件尾,接下来输入流被阻断。需要重置输入流,如果不重置,接下来将无法获取文件数据。
//获取CSV文件中数据的列数
fin.seekg(pos - 1, ios_base::beg); //返回到文件中有数据开始的那一行的首位
while (fin.get(ch))
{
if (ch == ',')
{
rowNum = rowNum + 1;
}
else
if (ch == '\n')
break;
}
rowNum = rowNum + 1;
//new创建二维动态数组
double **arr = new double *[lineNum];
for (int i = 0; i < lineNum; i++)
{
arr[i] = new double[rowNum];
}
//把CSV文件中的数据存入二维数组中
fin.seekg(pos - 1, ios_base::beg); //返回到文件中有数据开始的那一行的首位
buf.erase(0);
double temp;
for (int i = 0; i < lineNum; i++)
{
for (int j = 0; j < rowNum; j++)
{
while (fin.get(ch))
{
if (ch != ',' && ch != '\n')
buf += ch;
else
{
temp = atof(buf.c_str());
arr[i][j] = temp;
buf.erase(0);
break;
}
}
}
}
fin.close();
//创建一个结构对象,保存:指向当前数组的指针、当前数组行数、当前数组列数
arrInfo info;
info.arrName = arr;
info.lineNum = lineNum;
info.rowNum = rowNum;
return info; //返回结构
}
////用法示例:Visual Studio 2017 Community
//#include<iostream>
//#include<fstream>
//#include<string>
//#include<vector>
//#include<cstdio>
//#include<cstdlib>
//#include"modifyCSVfile.h"
//
//using namespace std;
//
//int main()
//{
// //读取CSV文件arr2中的数据,返回一个结构。结构包括:指向数组的指针、数组行数、数组列数
// modifyCSVfile<double> read;
// auto arr1 = read.CSVtoArray("arr2");
//
// //输出数组数据到屏幕
// for (int i = 0; i < arr1.lineNum; i++)
// {
// for (int j = 0; j < arr1.rowNum; j++)
// {
// cout << arr1.arrName[i][j] << " ";
// }
// cout << endl;
// }
//
// //注意:由于调用CSVtoArray函数时,生成的数组是用new分配的,所以数据用完之后要记得释放。
// //这里没有释放,可以调用delArray函数释放。
//
// system("pause");
// return 0;
//}
//*************************************************************************************************************************************************************
//*************************************************************************************************************************************************************
template<typename base_T>
int modifyCSVfile<base_T>::delArray(arrInfo info)
//函数功能:删除调用CSVtoArray函数时生成的动态二维数组
//由于CSVtoArray函数里边的二维数组是使用new创建的,所以用完之后要释放
{
for (int i = 0; i < info.lineNum; i++)
delete[] info.arrName[i];
delete[] info.arrName;
return 0;
}
////用法示例:Visual Studio 2017 Community
//#include<iostream>
//#include<fstream>
//#include<string>
//#include<vector>
//#include<cstdio>
//#include<cstdlib>
//#include"modifyCSVfile.h"
//
//using namespace std;
//
//int main()
//{
// //读取CSV文件arr2中的数据,返回一个结构。结构包括:指向数组的指针、数组行数、数组列数
// modifyCSVfile<double> read;
// auto arr1 = read.CSVtoArray("arr2");
//
// //输出数组数据到屏幕
// for (int i = 0; i < arr1.lineNum; i++)
// {
// for (int j = 0; j < arr1.rowNum; j++)
// {
// cout << arr1.arrName[i][j] << " ";
// }
// cout << endl;
// }
//
// //释放调用CSVtoArray函数时用new生成的动态二维数组
// modifyCSVfile<double> del;
// del.delArray(arr1);
//
// system("pause");
// return 0;
//}
未完 ......
点击访问原文(进入后根据右侧标签,快速定位到本文)
C++ 把数组数据存入 CSV 文件,以及读取 CSV 文件的数据的更多相关文章
- 从文件中读取yuv和h264数据
1.从文件中读取h264数据 参考ffmpeg avc.c写的从文件中一帧帧读取h.264数据的demo #include <stdio.h> #include <stdlib.h& ...
- spring 框架的xml文件如何读取properties文件数据
spring 框架的xml文件如何读取properties文件数据 第一步:在spring配置文件中 注意:value可以多配置几个properties文件 <bean id="pro ...
- c文件二进制读取写入文件、c语言实现二进制(01)转化成txt格式文本、c读取文件名可变
c语言实现二进制(01)转化成txt格式文本: 下面的程序只能实现ascall对应字符转换,如果文件内出现中文字符,则会出现错误. 本程序要自己创建个文本格式的输入文件a1.txt,编译后能将文本文件 ...
- java读取 500M 以上文件,java读取大文件
java 读取txt,java读取大文件 设置缓存大小BUFFER_SIZE ,Config.tempdatafile是文件地址 来源博客http://yijianfengvip.blog.163.c ...
- 通过文件路径读取CSV表格内的数据
ReadDataFromCSV.h UCLASS() class MYPROJECT_API UReadDataFromCSV : public UBlueprintFunctionLibrary { ...
- C#写入(覆盖形式)数据到CSV文件 和 读取CSV文件
/// <summary> /// 写入数据到CSV文件,覆盖形式 /// </summary> /// <param name="csvPath"& ...
- php操作文件(读取写入文件)
一,PHP如何读取文件 PHP读取文件可以读取当前服务器或远程服务器中的文件.其步骤是:打开文件.读文件和关闭文件. 1,PHP如何打开文件 使用PHP函数fopen()打开一个文件,fopen()一 ...
- Node.js——fs模块(文件系统),创建、删除目录(文件),读取写入文件流
/* 1. fs.stat 检测是文件还是目录(目录 文件是否存在) 2. fs.mkdir 创建目录 (创建之前先判断是否存在) 3. fs.writeFile 写入文件(文件不存在就创建,但不能创 ...
- [Python] python3 文件操作:从键盘输入、打开关闭文件、读取写入文件、重命名与删除文件等
1.从键盘输入 Python 2有两个内置的函数用于从标准输入读取数据,默认情况下来自键盘.这两个函数分别是:input()和raw_input(). Python 3中,不建议使用raw_input ...
- 在JavaScript文件中读取properties文件的方法
假设有JavaScript文件叫做:readproperties.js,这个文件需要读取config.properties这个配置文件,步骤如下: 1. 下载插件jquery.i18n.proper ...
随机推荐
- 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 ...
- BZOJ 5469: [FJOI2018]领导集团问题 dp+线段树合并
在 dp 问题中,如果发现可以用后缀最大值来进行转移的话可以考虑去查分这个后缀最大值. 这样的话可以用差分的方式来方便地进行维护 ~ #include <bits/stdc++.h> #d ...
- Vue之路由
1. SPA是什么 单页Web应用(single page application,SPA),就是只有一个Web页面的应用, 是加载单个HTML页面,并在用户与应用程序交互时动态更新该页面的Web应用 ...
- CSPS_110
永远不要相信出题人诸如“保证图联通”之类的鬼话. T1 最优情况一定为从LR最高的不同位以下全是1 T2 折半搜索 T3 1.我算法不是mlog^2m,最坏情况下mlogm再乘个根号m, 考试的时候没 ...
- 安卓入门教程(十五)- Fragment,Service,WAMP下载
Fragment概述 Fragment可以被嵌入到Activity中,一个Activity可以有多个Fragment. 创建Fragment public class MyFragment exten ...
- c博客作业-我的第一篇博客
1.你对网络专业或者计算机专业了解是怎样的? 以前接触计算机,只是把它当作娱乐的工具,并没有太过了解,现在我通过查阅了解了一些计算机的知识. 计算机专业的学生要学习的不仅是会使用,而且要学习计算机的基 ...
- URL的作用是什么?它由几部分组成?
URL是统一资源定位符,对可以从互联网上得到的资源的位置和访问方法的一种简洁的表示,是互联网上标准资源的地址.互联网上的每个文件都有一个唯一的URL,它包含的信息指出文件的位置以及浏览器应该怎么处理它 ...
- 【POJ2996】Help Me with the Game
题目传送门 本题知识点:模拟(如果对国际象棋不熟悉的同学可以先百度一下) 题意很简单,就是让我们找出白棋跟黑棋每枚棋子的位置,并要按照一定的顺序输出( K -> Q -> R -> ...
- hive集成kerberos
1.票据的生成 kdc服务器操作,生成用于hive身份验证的principal 1.1.创建principal # kadmin.local -q “addprinc -randkey hive/yj ...
- AT1879 2 つの山札
题面 题解 直接求解比较麻烦,考虑将问题进行转化. 设序列\(a = \{3, 1, 4, 2, 5\}, b = \{3, 2, 4, 1, 5\}\),那么我们构造一个正方形方格,将\(a\)放在 ...