1

I would like to read a .csv file into a c++ code and then convert the text data into a structure .. i can write a algorithm to read the .csv file but how will i save it into a structure... .. The data in the csv file is in the excel cells the 1st Column contains latitude , 2nd column contains longitude and 3rd contains the branch code and 4th contains the address and there are 7000 rows containing different locations and addresses.. i need to save these into a structure in C++ algorithm

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main () {
    string line;
    ofstream myfile;
    myfile.open("burgerking.csv");
    if(myfile.is_open()){
    cout<<"The file is open"<<endl;
    while (getline(myfile,line)) {
        cout << line << '\n';
        }
    }    
    myfile.close();
    system("PAUSE");
    return 0;
}
Nicholas Wilson
  • 9,435
  • 1
  • 41
  • 80
Murtaza
  • 27
  • 5
  • 4
    "How will you save it?" Very carefully, one supposes. Have you written any code? Do you have an idea what sort of data structure you need? Where exactly did you get stuck? – Joe Z Dec 13 '13 at 13:24
  • You don't want to serialize stuctures with addresses in. Each time you re-run your code the address will contain something different. – doctorlove Dec 13 '13 at 13:24
  • Unless he is referring to the cell address in Excel (A1, B1, C1...). Honestly, not a very clear question. – microslop Dec 13 '13 at 13:36
  • possible duplicate of [CSV parser in C++](http://stackoverflow.com/questions/1120140/csv-parser-in-c) – Tim Pierce Dec 13 '13 at 13:37
  • i was just trying to read the file but there is error in getline function i dont know why ... its a .csv file i think that should be the problem – Murtaza Dec 13 '13 at 13:39
  • error in getline` at compile time or runtime? Care to share some code? – doctorlove Dec 13 '13 at 13:47

2 Answers2

0

One (good) way to store interlinked structs is by using indices:

You cannot/shouldnot write into the file the struct at memory-address 0x12345678, but you CAN write this is struct 181 and its first reference goes to struct 912, its second ref goes to....

It can be done efficiently: have a hashmap that maps struct-pointers to indices you choose for them, every time you would write a pointer you instead write the index.

Bernd Elkemann
  • 23,242
  • 4
  • 37
  • 66
0
#include <iostream>
#include <sstream> 
#include <fstream> 
#include <vector> 
#include <string.h>
#include "link1.h"
using namespace std;

class struc{

        link1 l1;

    public:
    struc()
    {
        string lat,lon, addr;
         string state1="\0";
        string state;
        l1.link1();
    ifstream inFile ("burgerking.csv"); 
    string line; 
    int linenum = 0;
    while (getline (inFile, line)) 
    { 
    linenum++; 
    cout << "\nLine #" << linenum << ":" << endl; 
    istringstream linestream(line); 
    string item; 
    int itemnum = 0;
    while (getline (linestream, item, ',')) 
    { 
    lat=item;
    break;}
    while (getline (linestream, item, ',')) 
    { 

    lon=item; 
    break;
        }
    while (getline (linestream, item, ',')) 
    { 

    addr=item;
     break;
        }
    while (getline (linestream, item, ',')) 
    { 
    state=item; 

     break;

    // cout<<lat<<endl<<lon<<endl<<addr<<endl<<state<<endl;


     }
     if(state1==state)
     break;
     else
     l1.add(state);
     state1=state;


    }
    }
    void disp()
    {
l1.display();
}};
////// Hey man please contact me ASAP skype:mrwaleedahmed i have got the same task as a   project so need a little bit of help