文件输入没有循环遍历文件

时间:2013-05-09 23:05:54

标签: c++ file-io

我从一个名为Enemies.txt的文本文件中输入。但是有一个问题是它只打印出文件中的第一个敌人。

这是我的代码:

void Enemies :: loadEnemies()
{
    string fileName = "Enemies\\Enemies.txt";
    ifstream infile(fileName);
    string garbage;
    int loadEnemyID;
    string loadEnemyName;
    int loadEnemyStrength;
    int loadExperienceGain;
    string loadWeapon;
    string loadRoom;
    while(infile >> garbage >> garbage >> loadEnemyID
     >> garbage >> garbage >> garbage >> loadEnemyName
     >> garbage >> garbage >> loadEnemyStrength
     >> garbage >> garbage >> garbage >> loadExperienceGain
     >> garbage >> garbage >> garbage >> loadWeapon
     >> garbage >> garbage >> loadRoom >> garbage)
    {
    cout << "Enemy ID: \t\t" << loadEnemyID <<"\n";
    cout << "Enemy Name: \t\t"<< loadEnemyName << "\n";
    cout << "Enemy Strength: \t"<< loadEnemyStrength << "\n";
    cout << "Experience Gain: \t" << loadExperienceGain << "\n";
    cout << "Weapon: \t\t" << loadWeapon << "\n";
    cout << "Room: \t\t\t" << loadRoom << "\n";
    int id = weapon.getWeaponID();
    int weight = weapon.getWeight();
    int damage = weapon.getDamage();
    Weapons w1 (id,loadWeapon, weight,damage);
    int roomID = room.getRoomID();
    string roomExits = room.getRoomExits();
    Rooms r1 (roomID,loadRoom, roomExits);
    Enemies e1 (loadEnemyID,loadEnemyName,loadEnemyStrength,loadExperienceGain,w1,r1);
    enemiesList.Append(e1);
    }
}

Enemies.txt

Enemy ID: 1.
Enemy Name: Wolves.
Enemy Strength: 5.
Experience Gain: 186.
Weapon Name: Claws.
Room Name: 1.

Enemy ID: 2.
Enemy Name: Cave Bear.
Enemy Strength: 10.
Experience Gain: 302.
Weapon Name: Claws.
Room Name: 4.

Enemy ID: 3.
Enemy Name: Viking.
Enemy Strength: 6.
Experience Gain: 254.
Weapon Name: Longsword.
Room Name: 0.

Enemy ID: 4.
Enemy Name: Criminal.
Enemy Strength: 6.
Experience Gain: 198.
Weapon Name: War Axe.
Room Name: 0.

Enemy ID: 5.
Enemy Name: Ninja.
Enemy Strength: 6.
Experience Gain: 211.
Weapon Name: Katana.
Room Name: 0.

Enemy ID: 6.
Enemy Name: Wild Boar.
Enemy Strength: 3.
Experience Gain: 111.
Weapon Name: Claws.
Room Name: 0.

但有趣的是,我有类似的武器代码,而且效果很好。

void Weapons :: loadWeapons()
    {
    string fileName = "Weapons\\Weapons.txt";
    ifstream infile(fileName);
    string garbage;
    int loadWeaponID;
    string loadWeaponName;
    int loadDamage;
    int loadWeight;
    while(infile >> garbage >> loadWeaponID >> garbage >> garbage
        >> garbage >> loadWeaponName >> garbage >> loadDamage >> garbage
        >> garbage >> loadWeight >> garbage)
    {
        //cout << "Weapon ID: \t\t"<< loadWeaponID<< "\n";
        //cout << "Weapon Name: \t\t"<< loadWeaponName << "\n";
        //cout << "Damage: \t\t" << loadDamage <<"\n";
        //cout << "Weight: \t\t" << loadWeight << "\n";
        Weapons w1 (loadWeaponID,loadWeaponName,loadDamage,loadWeight);
        weaponsList.Append(w1);
    }
    }

Weapons.txt     ID:1。     武器名称:Katana。     伤害:20。     重量:6。

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

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

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

ID: 5.
Weapon Name: Staff.
Damage: 3.
Weight: 0.

1 个答案:

答案 0 :(得分:4)

在你的第二个也是最后一个敌人中,你有一个由两个单词组成的名字:

Enemy Name: Cave Bear.

当它试图读取下一行的强度时,这会弄乱解析。

我建议使用不同的解析方法,例如getlinesplit,而不是尝试滥用operator>>。这样,你可以在右侧放置任何你想要的东西,它也不会搞砸解析。

编辑:

此外,出于某种原因,您需要从解析中删除最后一个>> garbage,否则它也将无效。但是,我仍然强烈建议您切换到别的东西,因为这个奇怪的事情发生的事实表明它是多么容易破碎。


要做getlinesplit - 就像解析一样,你会做这样的事情:

std::string line;
while(std::getline(file, line)) {
    std::stringstream ss(line);
    std::string left, right;
    std::getline(ss, left, ':'); // Read until the colon
    std::getline(ss, right, '.'); // Read until the period
    std::cout << left << ": " << right << std::endl;
}