CSV文件读写

#include<string>
#include<iostream>
#include<fstream>
#include<sstream>
/********************************************************
 *  @function :  KVSaveAsCsvFile
 *  @brief    :  保存csv文件
*********************************************************/
inline void KVSaveAsCsvFile(const string& szFileName, const CObArray& ObjectArray)
{
	ofstream outFile;
	outFile.open(szFileName.c_str(), ios::ate|ios::out/*ios::app|ios::out*/); // 打开模式可省略
	int nCount = ObjectArray.GetCount();
	PTagAttribute pTagAttribute = NULL;
	string szTagValue = "";
	for (int nIndex = 0; nIndex < nCount - 1; ++nIndex)
	{
		pTagAttribute = (PTagAttribute)ObjectArray[nIndex];
		szTagValue = MesCStringToString(CString((char*)(System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(pTagAttribute->strTagValue)).ToPointer()));
		outFile << szTagValue << ",";
	}
	pTagAttribute = (PTagAttribute)ObjectArray[nCount - 1];
	szTagValue = MesCStringToString(CString((char*)(System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(pTagAttribute->strTagValue)).ToPointer()));
	outFile << szTagValue << "\n";
	outFile.close();
}

/********************************************************
 *  @function :  KVSaveAsCsvFile
 *  @brief    :  保存csv文件
*********************************************************/
inline void KVReadCsvFile(CObArray& ObjectArray)
{
	ifstream fin("data.csv");                        //打开文件流操作
	string strLineInfo = "";
	int nIndex = 0;
	while (getline(fin, strLineInfo))                //整行读取,换行符“\n”区分,遇到文件尾标志eof终止读取
	{
		istringstream sin(strLineInfo);              //将整行字符串line读入到字符串流istringstream中 
		string info = "";
		while (getline(sin, info, ','))              //将字符串流sin中的字符读入到Waypoints字符串中,以逗号为分隔符
		{
			PTagAttribute pTagAttribute = (PTagAttribute)ObjectArray[nIndex];
			pTagAttribute->strTagValue = gcnew String(info.c_str());
		}
	}
	fin.close();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值