首页 > 电脑

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

更新时间2018-05-08 23:35:24

文件内容是 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;
}


相关标签:谢谢

上一篇:jdbc连接数据库

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