【问题标题】:ifstream not reading input file correctlyifstream 没有正确读取输入文件
【发布时间】:2016-02-26 17:02:47
【问题描述】:

我有这个 fillTable() 函数,它使用 ifstream 从文件中读取输入。

void dgraph::fillTable()
{
char x = ' ';
slot = 0;
ifstream fin("table.txt");
while (fin >> Gtable[slot].vertexName)
{
    countUsed++;

    while (fin >> Gtable[slot].outDegree)
    {
        int y = 0;
        while (y < Gtable[slot].outDegree)
        {
            fin >> x;
            Gtable[slot].adjacentOnes.addRear(x);
            y++;
        }
    }
    slot++;
}
fin.close();
}

问题是主while循环只会执行一次。我需要它继续运行,直到文件中没有任何内容。到目前为止,我所能弄清楚的是,除了 0 之外没有其他索引似乎在工作。我尝试从索引 1 开始,顶点名称为 -52 'Ì',outDegree 为 -858993460。

    A 2 B F
    B 2 C G
    C 1 H
    E 0
    F 2 I E
    G 0
    H 2 G I
    I 3 A G E

这是我试图读入 Gtable 的输入文件。每一行的第一个字母是vertexName,数字是outDegree,每一行剩下的字母代表初始顶点的相邻顶点。基本上我从第一行得到所有东西,然后它跳出循环,我不确定为什么会这样。任何帮助表示赞赏。

【问题讨论】:

  • 哇哇哇!!!刚刚修复它我知道还没有人回答这个问题,但似乎我需要的只是添加一个休息时间;第二个while循环的语句。

标签: file loops input while-loop ifstream


【解决方案1】:

刚刚添加了中断;声明到第二个循环。

while (fin >> Gtable[slot].vertexName)
{
    countUsed++;

    while (fin >> Gtable[slot].outDegree)
    {
        int y = 0;
        while (y < Gtable[slot].outDegree)
        {
            fin >> x;
            Gtable[slot].adjacentOnes.addRear(x);
            y++;
        }
        break;
    }
        slot++;
}
fin.close();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-28
    • 1970-01-01
    • 2019-06-16
    • 2017-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-29
    相关资源
    最近更新 更多