从文本文件中读取时,如何知道我是否已经到达新行?

时间:2018-05-12 01:43:22

标签: c++ parsing

给出一个如下所示的文本文件:

-10 15 -50 -60 70 -30

0 1

1 2

1 3

3 4

4 5

“-10 15 -50 -60 70 -30”的第一行应该放入阵列。

其余的线应该用作邻接矩阵的连接点。

我如何知道自己是否已到达终点?到目前为止,这是我的代码。

string temp;
    while (getline(txtfile, temp, '\n')) {
        istringstream ss(temp);
        int num;
        while (ss >> num) {
            cout << num << endl;
        }
    }

我可以将所有数字分开但我想在到达终点后停止。

编辑代码:

ifstream txtfile("smallgraph.txt");
if (txtfile.is_open()) {
    cout << "Successfully opened file " << "graph.txt" << endl;
    int nodes = 0;
    int budget = 0;

    txtfile >> nodes >> budget;
    cout << nodes << " " << budget << endl;

    vector<int> firstLine;
    string temp;
    if (getline(txtfile, temp, '\n')) {
        istringstream ss(temp);
        int num;
        while (ss >> num)
            firstLine.push_back(num);
    }
    for (int i = 0; i < firstLine.size(); i++) {
        cout << firstLine[i] << " ";
    }
}

0 个答案:

没有答案
相关问题