0
我尝试采取从文本文件输入但在最后一刻,当我加载它在重复加载文本文件函数创建一个副本 - C++
**
- 这里是什么文本文件包含:
**
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);
}
}
真的......我见过这么多网站:S。我是否用cout打印文件?我把你发布的while循环用在我的代码中,它会跳过每一个第二个武器...... – Becca 2013-05-03 14:13:52
它在几乎很多网站上也是错误的。 – andre 2013-05-03 14:14:38
是的,这个很有趣http://mathbits.com/MathBits/CompSci/Files/End.htm给eof完全不正确的描述,然后承认它并不总是这样工作,甚至没有承认它们可能是说出他们的屁股。显然这是因为eof是'胡思乱想'! – john 2013-05-03 14:15:40