【发布时间】:2022-01-11 05:16:27
【问题描述】:
我正在尝试将文本文件中的这些数据类型添加到数组中,但出现超出范围的内存错误。文本文件如下所示:
1234,Chris Bobby,9/9/1999,123 Main Street,123-456-7890,5000.00
这是我的代码的样子:
void AddCustomersToArray(Customer *customers, fstream& customersFile) {
string line;
int i = 0;
string Number;
string FullName;
string DOB;
string Address;
string Telephone;
string Balance;
while (getline(customersFile, line)) {
stringstream ss(line);
getline(ss, Number, ',');
customers[i].Number = stoi(Number);
//cout << customers[i].Number << endl;
getline(ss, FullName, ',');
customers[i].FullName = FullName;
//cout << customers[i].FullName << endl;
getline(ss, DOB, ',');
customers[i].DOB = DOB;
//cout << customers[i].DOB << endl;
getline(ss, Address, ',');
customers[i].Address = Address;
//cout << customers[i].Address << endl;
getline(ss, Telephone, ',');
customers[i].Telephone = Telephone;
//cout << customers[i].Telephone << endl;
getline(ss, Balance, ',');
customers[i].Balance = stoi(Balance);
//cout << customers[i].Balance << endl;
i++;
}
【问题讨论】:
-
你能显示调用代码吗?我们需要查看您为
customers传递的内容。 -
你的数组大小是多少?该文件中有多少客户?
-
customers 是在全局范围内声明的客户结构数组,大小为 10。截至目前,该文件中有 2 个客户。这只是整个项目的一个 sn-p,因为后面还有一些函数可以让用户从文件中添加和删除客户。
-
也许您只有两个客户的文件超过十行?