gpt4 book ai didi

C++ - 无法将 CSV 解析到我的结构中

转载 作者:太空宇宙 更新时间:2023-11-04 14:30:21 24 4
gpt4 key购买 nike

我有一个格式如下的 CSV:

date,fruit,quantity1,quantity2,quantity3
2016-07-14,banana,3,20,6
2016-07-14,banana,3,50,15
2016-07-14,banana,0,25,15
2016-07-14,banana,3,25,6
2016-07-14,apple,3,10,20.5
2016-07-14,apple,0,30,5
2016-07-14,apple,0,5,30
2016-07-14,peach,3,10,30.2
2016-07-14,peach,3,40,4
2016-07-14,peach,3,10,12
2016-07-14,peach,0,10,8
2016-07-14,peach,3,200,3

我想解析这个文件并将它存储在一个结构中。但是我收到堆栈溢出错误。它究竟在哪里失败?是因为结构中的数据类型冲突吗?一些数据类型是 float ,我正在尝试使用 getline 和一个临时字符串变量来存储信息。

完整代码如下:

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

using namespace std;

struct FruitInventory
{
string date;
string fruit;
float quantity1;
float quantity2;
float quantity3;
};

int main()
{
ifstream myfile;
myfile.open("fruit_inventory.csv", ios::in);

string line;

FruitInventory todaysFruitSupply[15000];

int i = 0;

int lineCount = 0;

while (myfile.good())
{
getline(myfile, line);

stringstream mystream(line);

string temp;

if (i > 0) //ignore header line
{
getline(mystream, todaysFruitSupply[i].date, ',');
getline(mystream, todaysFruitSupply[i].fruit, ',');
getline(mystream, temp, ',');
todaysFruitSupply[i].quantity1 = stof(temp);
getline(mystream, temp, ',');
todaysFruitSupply[i].quantity2 = stof(temp);
getline(mystream, temp, ',');
todaysFruitSupply[i].quantity3 = stof(temp);
}

i++;
lineCount++;
}

myfile.close();

system("pause");

return 0;
}

编辑:它在文件的最后一行中断,因为有一个换行符。删除后,它现在完全执行。我如何才能确保它将来能够正确处理该问题?

最佳答案

这是一个作为局部变量分配的大对象:

FruitInventory todaysFruitSupply[15000];

这显然是堆栈溢出的原因。正如上面的评论所说,您应该考虑一个动态数据结构,例如 std::vector,它将根据需要增长并自动管理其内存。

std::vector<FruitInventory> todaysFruitSupply;

It was breaking on the last line of the file because there was a new-line character. After deleting that, it now executes completely. How can I make sure it can handle that correctly in the future?

当你读到一行时,你应该检查它是否为空:

while (myfile.good())
{
getline(myfile, line);
if (line.empty())
break;

或者更好的是,不要使用 keep using good() 而是测试输入操作的结果:

while (getline(myfile, line) && !line.empty())
{

整个事情看起来像:

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

using namespace std;

struct FruitInventory
{
string date;
string fruit;
float quantity1;
float quantity2;
float quantity3;
};

int main()
{
ifstream myfile;
myfile.open("fruit_inventory.csv", ios::in);

string line;

std::vector<FruitInventory> todaysFruitSupply;

int lineCount = 0;
getline(myfile, line); // ignore header line

FruitInventory inv;

while (getline(myfile, line) && !line.empty())
{
stringstream mystream(line);

string temp;
getline(mystream, inv.date, ',');
getline(mystream, inv.fruit, ',');
getline(mystream, temp, ',');
inv.quantity1 = stof(temp);
getline(mystream, temp, ',');
inv.quantity2 = stof(temp);
getline(mystream, temp, ',');
inv.quantity3 = stof(temp);
if (!mystream)
break; // something went wrong reading the line

todaysFruitSupply.push_back(inv);
lineCount++;
}
}

关于C++ - 无法将 CSV 解析到我的结构中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38402800/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com