0

我试图从文本文件中获取输入,但是当我加载它时,最后有一个副本。

**

  • 以下是文本文件包含的内容:

**

Weapon Name: Katana.
Damage: 20.
Weight: 6.

Weapon Name: Longsword.
Damage: 17.
Weight: 9.

Weapon Name: WarAxe.
Damage: 22.
Weight: 20.

Weapon Name: Staff.
Damage: 9.
Weight: 6.

**

  • 这是加载的内容:

**

Weapon Name: Katana.
Damage: 20.
Weight: 6.
Weapon Name: Longsword.
Damage: 17.
Weight: 9.
Weapon Name: WarAxe.
Damage: 22.
Weight: 20.
Weapon Name: Staff.
Damage: 9.
Weight: 6.
Weapon Name: Staff.
Damage: 9.
Weight: 6.

以下函数从文件中获取文本并将其添加到我编写的 DoublyLinkedList 类中。这是功能:

void Weapons :: getWeapon()
    {
    string fileName = "Weapons\\Weapons.txt";
    ifstream infile(fileName);
    string garbage;
    string weapon;
    int damage;
    int weight;
    while(infile.good())
    {
        infile>>garbage;
        infile>>garbage;
        infile>>weapon;
        infile>>garbage;
        infile>>damage;
        infile>>garbage;
        infile>>garbage;
        infile>>weight;
        infile>>garbage;
        cout << weapon << "\n" << damage << "\n" << weight << "\n";
        Weapons w1 (weapon,damage,weight);
        weaponsList.Append(w1);
    }
}
4

1 回答 1

13

还有一个while (file.eof())错误。以正确的方式做

while(infile >> garbage >> garbage >> weapon >> garbage >> 
      damage >> garbage >> garbage >> weight >> garbage)
{
     cout << weapon << "\n" << damage << "\n" << weight << "\n";
     Weapons w1 (weapon,damage,weight);
     weaponsList.Append(w1);
}

eof()不做你认为它做的事。这个要发多少次?

于 2013-05-03T14:10:01.473 回答