我试图从文本文件中获取输入,但是当我加载它时,最后有一个副本。
**
- 以下是文本文件包含的内容:
**
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);
}
}