简体   繁体   English

不会读取完整的文件 C++

[英]Won't read full file c++

I am trying to populate a linked list with creatures from a file.我正在尝试使用文件中的生物填充链接列表。 However, when I do it reads the first one but no other creatures.但是,当我这样做时,它会读取第一个但没有其他生物。 I cannot pass an int to get the number of creatures because it is not static.我不能通过 int 来获取生物的数量,因为它不是静态的。

I have the following code我有以下代码

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

void clearBuffer() {
    cin.clear();
    cin.ignore(100, '\n');
}

void enterMagicalCreatureFromFile() {
    string filepath = "test.txt";
    ifstream creatureFile;
    creatureFile.open(filepath);
    if (!creatureFile) {
        cerr << "Unable to open file " << filepath << endl;
        return;
    }
    string name, description;
    float cost;
    char danger;
    int numCreated = 0;
    while (getline(creatureFile, name) && getline(creatureFile, description) && creatureFile >> danger && creatureFile >> cost) {
        cout << "name: " << name << " desc: " << description << " danger: " << danger << " cost: " << cost << endl;
        numCreated++;
    }
    cout << numCreated << " creatures from " << filepath << " have been added to the zoo" << endl;
}

int main() {
    enterMagicalCreatureFromFile();
}

The file test.txt looks like文件 test.txt 看起来像

Beholder
Giant center eye and twelve eye stalks above it.  It is a flying eyeball.  Mouth full of razor sharp teeth.  Eye stalks shoot various beams of magical death-dealing energy.
1
750.85
Banshee
The English Banshee is a fairy woman who wails when death is approaching.They do not cause death, only mourn it.  Banshees are almost always female, and are usually seen with long, dark, black hair and pale chees.  Their eyes also are usually red from crying.
0
15.5
Troll
Ugly and big.  Sometimes smell bad.
1
85648.34
Mike Wazowski
One-eyed, funny green monster.  A scare assistant to James P. Sullivan at Monsters, Inc.  Mike doesn't want any interruptions in his life.
0
455.32
Unicorn
The unicorn is a legendary creature that has been described since antiquity as a beast with a single large, pointed, spiraling horn projecting from its forehead. 
0
24.32
Sasquatch
The sasquatch is also called Big Food.  Bigfoot is a cryptid in American folklore, supposedly a simian-like creature that inhabits forests, especially those of the Pacific Northwest. Bigfoot is usually described as a large, hairy, bipedal humanoid.
1
39475.93

As I said the first creature "Beholder" loads and get added to the list perfectly, so I don't think my logic is in the list or the creature file.正如我所说,第一个生物“Beholder”加载并完美地添加到列表中,所以我认为我的逻辑不在列表或生物文件中。

I am stuck, any pointers?我被卡住了,有什么指点吗?

After reading the double cost , there's a newline waiting in the creatureFile stream.读取 double cost ,在creatureFile流中有一个换行符在等待。

We need to advance past that with ignore() before reading the next creature:在阅读下一个生物之前,我们需要使用ignore()超越它:

while (std::getline(creatureFile, name)
       && std::getline(creatureFile, description)
       && creatureFile >> danger
       && creatureFile >> cost)
{
    std::cout << "name: " << name
              << " desc: " << description
              << " danger: " << danger
              << " cost: " << cost
              << std::endl;
    ++numCreated;
    creatureFile.ignore(100, '\n');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM