用C++算法将.csv文件转换为结构
Converting a .csv file into a structure in C++ algorithm
我想将.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
相关文章:
- JSON转换为nlohmann JSON-lib中的结构数组
- NLOHMANN 的 JSON 库将数组转换为结构向量
- 从成员指针到整个结构/类的强制转换
- 在nlohmann json中,如何将嵌套对象的数组转换为嵌套结构的向量
- 将字符数组转换为时间结构
- 使用带有链表的堆栈数据结构将中缀转换为后缀
- i2d_X509_REQ_INFO无法正确转换req_info结构
- 在C++中将结构转换和存储为二进制
- 将字符数组转换为结构时出现问题. 结构的字符数组变量溢出
- 转换包含向量 C++ 的语法和结构
- 正在转换结构数据的字节序
- 如何大致将 cpp 代码结构转换为 python
- 将结构 std::memcpy 转换为具有足够容量的 std::vector 是未定义的行为<char>吗?
- C++ 向下转换结构
- 尝试在C++中转换结构时出现奇怪的错误消息
- 向向下转换结构添加属性值会导致分段错误
- C++基于子类型动态强制转换结构
- 从字节缓冲区强制转换结构
- c++中的类型转换结构
- c++的类型转换结构指针指向类型值