我试图从文本文件中获取输入,但是当我加载它时,最后会有重复。
**
**
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);
}
}
答案 0 :(得分: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()
没有按照您的想法行事。这个必须发布多少次?