我无法弄清楚为什么输出会经历两次。
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();
输出如下:
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
它完成了我需要的东西,但它可以追溯到它并将它们归零。任何帮助都会很棒! 谢谢!
答案 0 :(得分:0)
efo()返回eofbit流状态标志,仅当您读取文件末尾时才会设置该标志。这发生在while循环的第二次迭代期间,您尝试将文件内容读入“output”。
如果在该行之后放置一个eof()检查,您将能够准确地突破所有循环(您必须使用可以在所有内部for循环中检查的本地标志变量)。