Hi ,
I want to declare an array of strings in my C++ code. But i am not getting the correct result.
My req. is to read from a csv file and then store each field in an array.
The csv file looks somehow like this:
sg,vsvs,22,53,dd,,,,dg
34,2,f,d,343,gg,45,,g,4
gdg,,3,fd,gd,3453,53,,,453,STRING1,,
aa,,sg,2,32fwef,3f3f3,f3,STRING1,,,,
Null fields are also there and should not be ignored. These null fields should also be pushed into the string array.
My code for the above req. is :
#include<iostream>
#include<fstream>
#include<string>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int main()
{
char* arr[50][50];
const int N=100;
char* toks=NULL;
char line[100];
int i=0,j=0;
static int row=0,col=0;
ifstream myfile("sample.csv");
while(myfile)
{
myfile.getline(line,N);
toks=strtok(line,",");
while(toks!=NULL)
{
arr[i][j]=toks;
toks=strtok(NULL,",");
j++;
col=j;
}
i++;
row=i-1;
j=0;
}
for(int i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
cout<<arr[i][j]<<"\t";
}
cout<<"\n";
}
myfile.close();
return 0;
}
But the result comes as :
,sg fwef f 3
I dont know y there are lots of spaces in between and also the desired result doesn't come.
Please anyone help me on this...
Thanks
Deepak