i have written a program to just detect the commas in a .csv file and copy the data into a structure ... but it also detects the comma in the text of each cell like if i have burgerking,AK in one cell then it will also detect the comma between burgerking and AK making it hard to copy the data in one cell to a string array in structure so can any one help me to copy the data present in each cell in .csv file into string file in a strcture in c++
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
struct burgerking //structure containing different strings for each column in .csv file
{
string longitude[7000];
string latitude[7000];
string location[7000];
string state[7000];
string address[7000];
};
void main () {
burgerking *burger;
burger= new burgerking();
string line;
ifstream myfile;
myfile.open("burgerking.csv"); //opening the csv file
if(myfile.good())
cout<<"File is Good to be opened"<<endl;
int l=0; //longitude
int n=1; //latutude
int e=2; //location
int ss=3; //state
int ad=4; //address
int j=0;
int b=0;
int kk=0;
int ll=0;
int add=0;
string line1;
string line2;
string line3;
for(int i=0;i<1500;i++)
{
getline(myfile,line,',');
if(i==0)
{
burger->longitude[j]=line;
j++;
l=l+7;
}
if(i==l)
{
burger->longitude[j]=line.substr(16,line.length());
j++;
l=l+7;
}
if(i==n)
{
burger->latitude[b]=line;
n=n+7;
b++;
}
if(e==i)
{
burger->location[kk]=line;
kk=kk+1;
e=e+7;
}
if(ss==i)
{
burger->state[ll]=line;
ss=ss+7;
ll++;
}
}
myfile.close();
myfile.open("burgerking.csv");
for(int c=0;c<2000;c++)
{
getline(myfile,line,',');
if(ad==c)
{
getline(myfile,line1,',');
getline(myfile,line2,',');
getline(myfile,line3,',');
line3=line3.substr(0,16);
burger->address[add]=line+','+line1+','+line2+','+line3;
add++;
ad=ad+4;
}
}
for(int k=0;k<300;k++)// loop just to check the program output
{
cout<<'\t'<<'\t'<<k+1<<endl;
cout<<burger->longitude[k]<<" ";
cout<<burger->latitude[k]<<" ";
cout<<burger->location[k]<<" ";
cout<<burger->state[k]<<" ";
cout<<burger->address[k]<<endl<<endl<<endl; //just to check the output
}
myfile.close();
system("PAUSE");
}