如何通过忽略单个字符的分隔行来从文件中读取整数的分隔行?

问题描述 投票:-1回答:1

我正在研究一个处理二进制搜索树的程序。我有一个看起来像这样的简单文本文件:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
$
5 4 3 5 7 13
$
11 6 7 2 12 6

第一行包含要插入树中的整数,第三行是要搜索的整数,第五行是要删除的整数。第二行和第四行用于标记其他行的分隔。它们包含单个字符“ $”。我在努力忽略第二和第四行。我有三个while循环,其中有两个忽略语句。经过测试,似乎从未进入第二和第三while循环。非常感谢您的帮助-下面是我的代码。

#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include "main.h"


int main(int argc, char *argv[])
{    
    string filename = "";
    ifstream inFile;

    filename = argv[1];
    inFile.open(filename.c_str());

    BST b;
    int num;

    while(inFile >> num)
    {
       b.insert(num);
       cout << num << endl; //output testing - the first line was successfully output so I think this part is fine
    }

    inFile.ignore(500, '$');

    while(inFile >> num) //this while loop is never entered
    {
        b.search(num);
    }
    inFile.ignore(500, '\n');

    while(inFile >> num) //this while loop is never entered
    {
        b.remove(num);
    }

    inFile.close();  
}
c++ while-loop binary-search-tree ifstream
1个回答
0
投票

完成读取数字的循环后,请添加一行以清除输入流的错误状态。否则,将不会执行后续的读取操作。

© www.soinside.com 2019 - 2024. All rights reserved.