将文本文件中的数据存储在结构数组 C++ 中
Storing data from a text file in an array of structures C++
我正在尝试将文本文件中的数据读入结构数组。 for 循环的第一次迭代读取并正确显示所有内容,直到达到布尔值,此后的每次迭代都不会按预期显示。最后的 bool 值是否导致整个文件的其余部分被错误读取?或者可能是 getline 引起的问题?
int main()
{
groceryProduct inventoryDatabase[25];
ifstream fin("inventory.txt");
if (!fin)
{
cout << "File could not be located.";
}
string itemName;
for (int index = 0; index < 25; index++)
{
getline(fin, inventoryDatabase[index].itemName, '\n');
fin >> inventoryDatabase[index].itemNumber;
fin >> inventoryDatabase[index].itemPrice;
fin >> inventoryDatabase[index].membershipPrice;
fin >> inventoryDatabase[index].payByWeight;
cout << inventoryDatabase[index].itemName << endl;
cout << inventoryDatabase[index].itemNumber << endl;
cout << inventoryDatabase[index].itemPrice << endl;
cout << inventoryDatabase[index].membershipPrice << endl;
cout << inventoryDatabase[index].payByWeight << endl;
}
return 0;
};
结构:
struct groceryProduct
{
double itemPrice;
double membershipPrice;
double itemWeight;
int itemQuantity;
string itemNumber;
string itemName;
bool payByWeight;
};
输出:
Apple
P0000
0.85
0.8
204 (expected output of 'false' instead of 204)
第一次迭代后循环每次迭代的输出:
-9.25596e+61
-9.25596e+61
204
谢谢,如果您需要更多信息,请告诉我。
文件:
Apple
P0000
.85
.80
false
Orange
P0001
.95
.85
false
Lemon
P0002
.65
.60
false
以下是我发现的一些可能导致您遇到问题的原因。
1) 数组没有 "magically filled" 数据。您有一个未初始化的数组,这意味着其中的数据尚不存在。完全没有。
要解决此问题,您必须在每次循环迭代开始时向数组添加一个新的 结构实例 。
我怎么发现的?好的经验法则:如果它很奇怪,它与内存有关。确保您已初始化所有内容。
2) 我看到当你同时使用 getline
和 <<
时会发生奇怪的事情。您使用 getline
而不是 <<
有什么特别的原因吗?
(我需要重新研究如何解决这个问题。我以前在我的 C++ class 中经常碰到它。)
3) 1201ProgramAlarm 说的完全正确
旁注:不要养成乱扔 double
的习惯,因为 "I want to be able to arbitrarily throw a large value in there." 这是一个浪费 space 的坏习惯,因为 double
是原来的两倍作为 float
.
了解 float
和 double
之间的区别 - 在科学情况之外,您几乎永远不需要 double
,因为它适用于有很多小数位的数字。 (这过于简单化了。)如果您一直使用 double
而不是 float
,那么您使用的内存是所需内存的两倍——事实上,每个变量多了 32 位。它加起来。 (人们想知道为什么现代程序需要 8GB 的 RAM 来完成与使用 100MB-RAM 的前辈相同的事情...)
价格总是有两位(很少有三位)小数位,因此 float
应该在所有情况下都非常适合。与权重相同。
您需要告诉您的流布尔值是带有
的文本
fin >> boolalpha >> inventoryDatabase[index].payByWeight
您在第一个 bool 输入后看到垃圾数据,因为在流中设置了 failbit,并且在它被重置之前不会有进一步的输入。这会导致您的数组数据保持未初始化状态。
我正在尝试将文本文件中的数据读入结构数组。 for 循环的第一次迭代读取并正确显示所有内容,直到达到布尔值,此后的每次迭代都不会按预期显示。最后的 bool 值是否导致整个文件的其余部分被错误读取?或者可能是 getline 引起的问题?
int main()
{
groceryProduct inventoryDatabase[25];
ifstream fin("inventory.txt");
if (!fin)
{
cout << "File could not be located.";
}
string itemName;
for (int index = 0; index < 25; index++)
{
getline(fin, inventoryDatabase[index].itemName, '\n');
fin >> inventoryDatabase[index].itemNumber;
fin >> inventoryDatabase[index].itemPrice;
fin >> inventoryDatabase[index].membershipPrice;
fin >> inventoryDatabase[index].payByWeight;
cout << inventoryDatabase[index].itemName << endl;
cout << inventoryDatabase[index].itemNumber << endl;
cout << inventoryDatabase[index].itemPrice << endl;
cout << inventoryDatabase[index].membershipPrice << endl;
cout << inventoryDatabase[index].payByWeight << endl;
}
return 0;
};
结构:
struct groceryProduct
{
double itemPrice;
double membershipPrice;
double itemWeight;
int itemQuantity;
string itemNumber;
string itemName;
bool payByWeight;
};
输出:
Apple
P0000
0.85
0.8
204 (expected output of 'false' instead of 204)
第一次迭代后循环每次迭代的输出:
-9.25596e+61
-9.25596e+61
204
谢谢,如果您需要更多信息,请告诉我。
文件:
Apple
P0000
.85
.80
false
Orange
P0001
.95
.85
false
Lemon
P0002
.65
.60
false
以下是我发现的一些可能导致您遇到问题的原因。
1) 数组没有 "magically filled" 数据。您有一个未初始化的数组,这意味着其中的数据尚不存在。完全没有。
要解决此问题,您必须在每次循环迭代开始时向数组添加一个新的 结构实例 。
我怎么发现的?好的经验法则:如果它很奇怪,它与内存有关。确保您已初始化所有内容。
2) 我看到当你同时使用 getline
和 <<
时会发生奇怪的事情。您使用 getline
而不是 <<
有什么特别的原因吗?
(我需要重新研究如何解决这个问题。我以前在我的 C++ class 中经常碰到它。)
3) 1201ProgramAlarm 说的完全正确
旁注:不要养成乱扔 double
的习惯,因为 "I want to be able to arbitrarily throw a large value in there." 这是一个浪费 space 的坏习惯,因为 double
是原来的两倍作为 float
.
了解 float
和 double
之间的区别 - 在科学情况之外,您几乎永远不需要 double
,因为它适用于有很多小数位的数字。 (这过于简单化了。)如果您一直使用 double
而不是 float
,那么您使用的内存是所需内存的两倍——事实上,每个变量多了 32 位。它加起来。 (人们想知道为什么现代程序需要 8GB 的 RAM 来完成与使用 100MB-RAM 的前辈相同的事情...)
价格总是有两位(很少有三位)小数位,因此 float
应该在所有情况下都非常适合。与权重相同。
您需要告诉您的流布尔值是带有
的文本fin >> boolalpha >> inventoryDatabase[index].payByWeight
您在第一个 bool 输入后看到垃圾数据,因为在流中设置了 failbit,并且在它被重置之前不会有进一步的输入。这会导致您的数组数据保持未初始化状态。