简体   繁体   English

将文本文件中的数据存储在结构数组C ++中

[英]Storing data from a text file in an array of structures C++

I am attempting to read data from a text file into an array of structures. 我正在尝试将数据从文本文件读取到结构数组中。 The first iteration of the for-loop reads and displays everything correctly until it reaches the Boolean value, and every iteration after that does not display as expected. for循环的第一次迭代会正确读取并显示所有内容,直到达到布尔值为止,此后的每次迭代都不会按预期显示。 Is the bool value at the end causing the entire rest of the file to be read incorrectly? 末尾的bool值是否导致文件的其余部分无法正确读取? Or perhaps an issue stemming from getline? 还是源于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;
};

The structure: 结构:

struct groceryProduct
{
    double itemPrice;
    double membershipPrice;
    double itemWeight;
    int itemQuantity;
    string itemNumber;
    string itemName;
    bool payByWeight;
};

The output: 输出:

Apple
P0000
0.85
0.8
204 (expected output of 'false' instead of 204)

Output for every iteration of loop after first iteration: 第一次迭代后循环的每次迭代的输出:

-9.25596e+61
-9.25596e+61
204

Thank you, and please let me know if you require any more information. 谢谢,如果您需要更多信息,请告诉我。

File: 文件:

Apple
P0000
.85
.80
false
Orange
P0001
.95
.85
false
Lemon
P0002
.65
.60
false

You need to tell your stream that the bool values are text with 您需要告诉您的流,布尔值是带有

fin >> boolalpha >> inventoryDatabase[index].payByWeight

You're seeing garbage data after the first bool input because failbit gets set in the stream and no further inputs will work until it is reset. 在第一个布尔输入之后,您会看到垃圾数据,因为在流中设置了故障位,并且直到重置它之后,其他输入才起作用。 This results in you array's data staying uninitialized. 这导致数组的数据保持未初始化状态。

Here are a couple of things I see that may be causing your problem. 我认为有几件事可能会引起您的问题。

1) An array is not "magically filled" with data. 1)数组没有被数据“神奇地填充”。 You have an uninitialized array, meaning that the data inside of it does not yet exist. 您有一个未初始化的数组,这意味着其中的数据尚不存在。 At all. 完全没有

What you have to do to remedy this is to add a new instance of the struct to the array at the start of each loop iteration. 要解决此问题,您需要做的是在每次循环迭代开始时将结构的新实例添加到数组中。

How'd I spot that? 我怎么发现的? Good rule of thumb: if it's weird, it's memory-related . 好的经验法则: 如果很奇怪,那就是与内存相关的 Make sure you've initialized everything. 确保已初始化所有内容。

2) I've seen weird things happen when you use getline and << next to each other. 2)我发现当您同时使用getline<<时,会发生奇怪的事情。 Is there a particular reason you are using getline over << ? 您是否在<<上使用getline是否有特定原因?

(I would need to re-research how to work around that weirdness. I used to hit it a lot in my C++ class way back when.) (我将需要重新研究如何解决这种怪异。我曾经在C ++类中经常遇到该问题。)

3) What 1201ProgramAlarm said is absolutely correct. 3)1201ProgramAlarm所说的完全正确。


Side note: Do NOT get into the habit of throwing double around because "I want to be able to arbitrarily throw a large value in there." 附注:不要进入投掷的习惯double左右,因为“我希望能在那里随意抛出一个大的价值。” It's a bad habit that wastes space, as double is twice as large as float . 这是一个不好的习惯,浪费空间,因为double是两倍大float

Learn the difference between float and double - you will almost never need double outside of scientific situations, because it is for numbers with a LOT of decimal places. 了解floatdouble float的区别-在科学情况下,您几乎永远不需要double float数,因为它适用于很多小数位的数字。 (That's oversimplifying it.) If you're using double over float all the time, you're using twice the memory you need - 32 bits per variable extra, in fact. (这过于简化了。)如果您一直在使用double over float ,则使用的内存是您需要的两倍-实际上,每个变量多了32位。 It adds up. 它加起来。 (And people wonder why modern programs need 8GB of RAM to do the same thing as their 100MB-RAM-using predecessors...) (人们想知道为什么现代程序需要8GB RAM才能与使用100MB RAM的前辈做相同的事情……)

Prices always have two (rarely three) decimal places, so float should fit that perfectly in all cases. 价格始终有两个(很少有三个)小数位,因此float在所有情况下都应该完全适合。 Same with weights. 重量也一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM