C++ 为什么要打印两次?

C++ 为什么要打印两次?,c++,for-loop,cout,C++,For Loop,Cout,我不明白为什么输出要经过两次 int lines = 3 myReadFile.open("graph.txt"); if (myReadFile.is_open()) { //Read in each value one at a time while (!myReadFile.eof()) { for(int i = 0; i < lines; i++) { for(int

我不明白为什么输出要经过两次

    int lines = 3
    myReadFile.open("graph.txt");
    if (myReadFile.is_open()) {

        //Read in each value one at a time
        while (!myReadFile.eof()) {
            for(int i = 0; i < lines; i++) {
                for(int j = 0; j<lines; j++) {
                    myReadFile >> output;
                    output2 = atoi(output);
                    Graph[i][j] = output2;
                    cout << "Graph[" << i <<"][" << j <<"] = " << output2 << endl;
                }
            //cout << output << output2 << endl; 
            }
        }

    } else {
        cout << "graph.txt does not exist." << endl; 
    }
    myReadFile.close();
它做了我需要它做的,但是它返回并将它们归零。任何帮助都会很好! 谢谢

efo()返回eofbit流状态标志,仅当读取超过文件末尾时才会设置该标志。这发生在while循环的第二次迭代中,您尝试将文件内容读入“output”


如果您在该行之后进行EFF()检查,您将能够准确地跳出所有的循环(您必须使用本地标志变量,您可以在所有内部循环中检查)。编辑:我点击了链接。现在看看这个链接,我想它解释得很好。有什么你不明白的吗?关键是要确保您的输入在使用它之前成功。@DDukesterman和这个:

myReadFile>>输出
完全取消选中没有多大帮助。谢谢!我现在明白了!评论旁边的向上箭头是我能为您做的最好的吗?
Graph[0][0] = 0
Graph[0][1] = 65
Graph[0][2] = 4
Graph[1][0] = 7
Graph[1][1] = 0
Graph[1][2] = 68
Graph[2][0] = 67
Graph[2][1] = 84
Graph[2][2] = 0
Graph[0][0] = 0
Graph[0][1] = 0
Graph[0][2] = 0
Graph[1][0] = 0
Graph[1][1] = 0
Graph[1][2] = 0
Graph[2][0] = 0
Graph[2][1] = 0
Graph[2][2] = 0