Dear All,
I have 2 cvs file like this:
<spec.csv>
Type,sybType,ID
CASH,ON,1
FRA,1x4,2
...
<data.csv>
ID,Rate
1,3
2,5.23
...
I want to read the files and put them into a 2D array. I am new to c++, pls help to give me some simple code to do this.
The follow are some of my current code to read one csv file, but still not success.
Thanks.
I have 2 cvs file like this:
<spec.csv>
Type,sybType,ID
CASH,ON,1
FRA,1x4,2
...
<data.csv>
ID,Rate
1,3
2,5.23
...
I want to read the files and put them into a 2D array. I am new to c++, pls help to give me some simple code to do this.
The follow are some of my current code to read one csv file, but still not success.
Code:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#define DELIM ","
using namespace std;
int main(int argc, char *argv[])
{
ifstream specf, dataf;
int specId[50],dataId[50];
char *buff,*sep;
char *InstrumentType[50],*subType[50];
int i=0,j=0;
specf.open(argv[1]); //Open Spec File
if (specf.is_open())
{
while(specf.getline(buff,256))
{
sep = strtok(buff, DELIM);
while (sep != NULL)
{
if(i==0)
{
InstrumentType[j]=sep;
cout<<j<<"IT="<<InstrumentType[j]<<endl;
i++;
}
else if(i==1)
{
subType[j]=sep;
cout<<j<<"ST="<<subType[j]<<endl;
i++;
}
else if(i==2)
{
specId[j]=atoi(sep);
cout<<j<<"ID="<<specId[j]<<endl;
i=0;
}
sep = strtok(NULL, DELIM);
}
j++;
}
}
else
{
cout <<"Error opening file "<<argv[1]<<"\n";
return -1;
}
for(j=0;j<10;j++)
{
cout<<j<<":IT="<<InstrumentType[j]<<":ST="<<subType[j]<<":ID="<<specId[j]<<endl;
}
}
Comment