c++读写操作CSV文件

/***************************************************************************************************
文件作用:
        CSV数据文件的处理
开发环境:
        Win10+STL
时间地点:
        文津楼 2017.4.24
作    者:
         九月
****************************************************************************************************/
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
#include <sstream>     

using namespace std;

int main(int argc, char* argv[])

{

	   std::string  strFileName = "D:\\wang_csv\\test3.csv";   //【0】文件名
       std::fstream file;                                     //【1】声明一个文件输入输出流对象
	   file.open(strFileName.c_str(),std::ios::in);           //【2】以读文件的方式打开文件
	   if(!file.is_open())
	   {
		   std::cout<<"【NOTICE】The file is loaded unsuccessfully!"<<std::endl;
		   std::system("pause");
	   }
	   else
	   {
		   std::cout<<"【NOTICE】The file is loaded successfully and the file is being read........"<<std::endl;
		   std::cout<<"【NOTICE】Press any key to continue,please!"<<std::endl;
		   std::system("pause");

	   }

	   std::string                        strTemp;
	   std::vector<string>                strLinesBuffer;                          //【1】以行的形式存储CSV文件读入的内容
	   std::vector<string>                strAll;                                  //【2】以单个字段的形式存储CSV文件中所有的内容
	   std::vector<std::vector<string>>   strTable;                                //【3】存储表格中的内容


	   int                   iRows     = 0;                                        //【1】CSV文件中的行数
	   int                   iCols     = 0;                                        //【2】CSV文件中的列数

	   while(getline(file,strTemp))
	   {
		   strLinesBuffer.push_back(strTemp);
		   std::cout<<strTemp<<std::endl;
		   char*  tmpString;
		   tmpString=strtok((char*)strTemp.c_str(),";");

		   while (tmpString)
		   {
			  if(!strcmp(tmpString,""))
			   {
				   std::system("pause");
			   }
			   else
			   {
				   strAll.push_back(tmpString);
			       std::cout<<tmpString<<std::endl;
			   }
			   tmpString = strtok(NULL,";");
		   }
		   strTable.push_back(strAll);
	   }
	   iRows = strTable.size();
	   iCols = strAll.size()/iRows;
	   std::cout<<"[1]CSV文件中行的总数"<<iRows<<std::endl;
	   std::cout<<"[2]CSV文件中列的总数"<<iCols<<std::endl;


	   double dSum = 0;
	   std::vector<double>   doubleTable;

	   /***********************************************************************************************************
	   模块说明:
	            将读入的CSV表格(字符串)格式-----转化为-----可以用于计算的double型表格
	   ************************************************************************************************************/
	   for(int i=0;i<53;i++)
	   {
		   for(int j=0;j<11;j++)
		   {
			   std::string tmp = (strAll[i*iRows+j]);
			   std::istringstream  isStrTmp(tmp);
			   double   num = 0.0;
			   isStrTmp>>num;
			   std::printf("%f",num);
			   doubleTable.push_back(num);
		   }
		   std::cout<<std::endl;
	   }
	   /***********************************************************************************************************
	   模块说明:
	            1)以上模块已经将CSV中的数据准备完毕
				2)已经将数据转化为可用于计算的double型数据,并且,数据以单纬动态数组的形式存储于doubleTable
	   ************************************************************************************************************/
	   double   sum     = 0.0;
	   int      iCalCol = 1;                                        //【1】待计算第几列的数据,列标号
	   for(int i=0;i<50;i++)
	   {
		   for(int j=0;j<11;j++)
		   {
			   if(j == iCalCol)
			   {
				   sum += doubleTable[i*iRows+j];
			   }
		   }
		   //std::cout<<std::endl;
	   }
	   std::cout<<"[Output Information]第"<<iCalCol<<"列的平均值 = "<<sum/iRows<<std::endl;
	   std::system("pause");
	   return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值