用C++算法将.csv文件转换为结构

Converting a .csv file into a structure in C++ algorithm

本文关键字:转换 结构 文件 csv C++ 算法      更新时间:2023-10-16

我想将.csv文件读取为c++代码,然后将文本数据转换为结构。。我可以写一个算法来读取.csv文件,但如何将其保存到结构中。。csv文件中的数据在excel单元格中,第一列包含纬度,第二列包含经度,第三列包含分支代码,第四列包含地址,共有7000行包含不同的位置和地址。。我需要将这些保存到C++算法中的一个结构中

#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;
}

存储互连结构的一种(好)方法是使用索引:

您不能/不应该写入文件the struct at memory-address 0x12345678,但可以写入this is struct 181 and its first reference goes to struct 912, its second ref goes to...

这可以有效地完成:有一个hashmap,它将结构指针映射到您为它们选择的索引,每次您编写指针时,您都会编写索引。

#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="";
        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