0

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");

}

Murtaza
  • 27
  • 5
  • possible duplicate of [CSV parser in C++](http://stackoverflow.com/questions/1120140/csv-parser-in-c) – ArtemGr Dec 15 '13 at 10:24
  • @ArtemGr i need an easy answer as i am a beginner... – Murtaza Dec 15 '13 at 10:27
  • This site already has lots of easy answers to your question. Please follow the guidelines: http://stackoverflow.com/questions/how-to-ask – ArtemGr Dec 15 '13 at 10:46
  • If you need to support embedded commas in the input then you generally want to choose a different delimiter or enclose the data in quotes. – Retired Ninja Dec 15 '13 at 10:52

1 Answers1

0

I would say that you are going about it all wrong.

To start with your structures should not contain arrays of strings, instead you should have one structure instance per store, and then store them in a std::vector:

So

struct burgerking
{
    std::string longitude;
    std::string latitude;
    std::string location;
    std::string state;
    std::string address;
};

and

std::vector<burgerking> stores;

Then when reading the file, you use std::getline to get a complete line, and use std::istringstream with std::getline to get the separate values:

std::string line;
while (std::getline(myfile, line))
{
    std::istringstream iss(line);

    burgerking burger;  // No pointers needed

    std::getline(iss, burger.longitude, ',');
    std::getline(iss, burger.latitude, ',');
    std::getline(iss, burger.location, ',');
    std::getline(iss, burger.state, ',');
    std::getline(iss, burger.address);   // Last field, no separator needed

    stores.push_back(burger);  // Add to collection
}

And finally when you're printing them out, use e.g.

for (const burgerking& b : stores)
{
    std::cout << "Longitude: " << b.longitude << '\n';
    // Etc.
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • The file i am working on .csv contains 7081 rows and 4 columns and data in each cell also has commas in it ... so will those commas be also read by the program or only the commas which are separating the columns will be read ? – Murtaza Dec 15 '13 at 10:32
  • @Murtaza Then you might have to modify the parsing to fit your needs better (substrings and such), but my answer should provide enough of a base to build on. – Some programmer dude Dec 15 '13 at 10:36
  • getting an error on this line for (const burgerking& b : stores) it says reference variable b requires an initializer... – Murtaza Dec 15 '13 at 10:43