为什么这个印两次
Why does this print twice?
我不明白为什么输出要经过两次。
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
它做了我需要它做的事,但它会返回并将它们清零。任何帮助都会很棒!谢谢
efo()返回eofbit流状态标志,只有在读取超过文件末尾时才会设置该标志。这种情况发生在while循环的第二次迭代中,您试图将文件内容读取到"output"中。
如果在该行之后进行eof()检查,您将能够准确地断开所有循环(您必须使用一个本地标志变量,您可以在所有内部for循环中进行检查)。
相关文章:
- 为什么映射插入和 map.find() 的单次迭代比插入和 map.find() 的两次单独迭代慢得多
- 为什么参数在构造 std::thread 时移动两次
- 为什么具有静态存储持续时间的同一内联变量在包含在 VS2017 编译的两个翻译单元中时会构造和销毁两次
- 为什么要执行两次位移((x >> 4)<< 4)?
- 为什么转换运算符调用复制构造函数两次,而等效函数只调用它一次
- 在钻石问题的求解中,为什么要虚拟地继承两次grand-parent类
- 为什么same_as概念检查两次类型相等
- 为什么与<运算符的向量比较会比较每个项目两次?
- 为什么这个非递归互斥锁可以获取两次?
- 为什么这个自定义分配器的析构函数在 GCC/MSVS 的 stdlib 中被调用两次
- 重载运算符 new(),为什么构造函数被调用两次?
- 为什么常量成员可以初始化两次?
- 为什么我需要两次相同的功能
- 为什么在下面的代码中调用复制构造函数两次
- 为什么我的串行串行在Arduino IDE上通过串行显示器进行两次打印
- 为什么当我在同一内存中各有两个指针时,我必须调用"delete"两次?
- 为什么在这里调用析构函数两次
- 为什么在打印消息时名称会打印两次?
- 为什么这个构造函数被调用两次
- 为什么我的对象似乎被创建了两次