我正在尝试将数据从csv文件读取到c++中。尽管程序已编译,但没有输出。当我运行调试器时,我发现有一些“未处理的异常”。其中一项声明存在堆栈溢出。另一个写着"0xC0000005:访问冲突读取位置0x001000000“。我真的不确定这些是什么意思,但当我用较小的数据集测试一个非常相似的程序时,它起作用了。
在我当前的代码中,我声明了12个数组,每个数组代表一列。每个数组包含537578个元素,代表每一行。
int raw_num = 537578;
int num = 537577;
std::string raw_User_ID[537578];
std::string raw_Product_ID[537578];
std::string raw_Gender[537578];
std::string raw_age[537578];
std::string raw_Occupation[537578];
std::string raw_City_Category[537578];
std::string raw_Stay_In_Current_City_Years[537578];
std::string raw_Marital_Status[537578];
std::string raw_Product_Category_1[537578];
std::string raw_Product_Category_2[537578];
std::string raw_Product_Category_3[537578];
std::string raw_Purchase[537578];下面的/*数组用于后面的数据类型转换,但在本部分中不使用*/
double User_ID[537577];
std::string Product_ID[537577];
char Gender[537577];
std::string age[537577];
int Occupation[537577];
char City_Category[537577];
std::string NumYearsInCity[537577];
bool Marital_Status[537577];
int Product_Category_1[537577];
int Product_Category_2[537577];
int Product_Category_3[537577];
double Purchase[537577];
std::ifstream infile;
infile.open("BlackFriday.csv");
if (!infile.is_open()) {
std::cout << "File not found" << std::endl;
}
else {
int count = 0;
while (!infile.eof()) {
std::getline(infile, raw_User_ID[count], ',');
std::getline(infile, raw_Product_ID[count], ',');
std::getline(infile, raw_Gender[count], ',');
std::getline(infile, raw_age[count], ',');
std::getline(infile, raw_Occupation[count], ',');
std::getline(infile, raw_City_Category[count], ',');
std::getline(infile, raw_Stay_In_Current_City_Years[count], ',');
std::getline(infile, raw_Marital_Status[count], ',');
std::getline(infile, raw_Product_Category_1[count], ',');
std::getline(infile, raw_Product_Category_2[count], ',');
std::getline(infile, raw_Product_Category_3[count], ',');
std::getline(infile, raw_Purchase[count], '\n');
count++;
}
}我输出了几个数组元素以确保数据输入正确,但没有输出。此外,代码退出-1073741571,而不是0。
发布于 2019-04-17 13:41:58
堆栈溢出意味着您分配的内存超过了堆栈中可用的内存,因此应用程序通常会因此而终止。应该在堆上分配大型数组您可以使用指针来实现这一点,但是如果您没有任何限制,我建议使用std::vector<std::string> product_id(537577);而不是std::string ...。您可以像对待数组一样对待向量,向量将为您执行内存管理。
https://stackoverflow.com/questions/55719973
复制相似问题