请问c++如何实现将一个csv文件根据不同的分组分割成多个文件,求指导~~谢谢~ - 爱问答

(爱问答)

请问c++如何实现将一个csv文件根据不同的分组分割成多个文件,求指导~~谢谢~

文件内容是 00001,2,2,2,2,2...............................,2

                   00002,2,2,2,2,2...............................,2

                   00001,0,0,0,0,0................................,0

                   00003,0,0,0,0,0................................,0

我想根据前面的00001这一组数据进行分割,将所有第一列相同的数据放在同一个文件中.

参考程序

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
struct DATA
{
string filename; //文件名
ofstream  *out; //文件名句柄
} ;

int main()
{
DATA tm;
vector <DATA> v; //用结构以方便查找等
vector <DATA>::iterator t;
string s,d,f;
int p,i;
ifstream fin("test.txt");
while(fin >> s)
 {
  p=s.find(','); //找到第一个逗号
  f=s.substr(0,p); //取文件名
  d=s.substr(p+1); //取数据
  cout << f <<" " << d <<  endl; //只是测试
  for(t=v.begin(); t!=v.end(); t++)
   {
    if (t->filename==f)
     break;
   }
  if (t!=v.end()) //文件已存在
   {
    *(t->out) <<  d << endl;
   }
  else //文件不存在
   {
    tm.filename=f;
    tm.out = new ofstream(f.c_str());
    *(tm.out) << d << endl;
    v.push_back(tm);
   }
 }
for(t=v.begin(); t!=v.end(); t++)
 {
  (t->out)->close();
  delete t->out;
 }
fin.close();
return 0;
}


相关标签:谢谢

下一篇:opencv识别行人后能发出提示吗

上一篇:jdbc连接数据库

热门标签:
excel 网盘 破解 word dll
最新更新:
微软重新评估新的Outlook的使用时机 联想推出搭载联发科Helio G80芯片组的Tab M9平板 英特尔创新大赛时间确定! 微软Edge浏览器在稳定渠道中推出Workspaces功能 英伟达RTX4060TiGPU推出MaxSun动漫主题! 谷歌地图为用户提供了街景服务! GameSir 在T4 Kaleid中推出了一款出色的控制器! 微软开始在Windows 11 中测试其画图应用程序的新深色模式! LG电子推出全球首款无线OLED电视 英伟达人工智能芯片崭露头角! Steam Deck可以玩什么游戏-Steam Deck价格限时优惠 雷蛇推出CobraPro鼠标 Kindle电子阅读器可以访问谷歌商店吗 Windows10如何加入组策略 window10图片查看器怎么没有了?