如果我有一个文件,有一个64 * 64整数的表。 (第一个64将是第0行;第64个将是第1行,依此类推)。如何将该表格存储到二维数组中。 这里是我的代码从一个文件存储64 * 64整数到一个2D数组
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream infile;
infile.open("table.txt");
if (infile.fail())
{
cout << "could not open file" << endl;
exit(6);
}
int array[63][63];
while (!infile.eof())
{
infile >> array[63][63];
}
cout << array[63][63] << endl;
return 0;
}
时就执行我只得到“1”
每次while循环迭代,要指定infile中导致成数组[63] [63],而不是改变X,Y(或任何索引)并填充整个数组。 –
对此,[这是错误的:'while(!infile.eof())'](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-错误)。而且,你总是读取* same *元素,每次迭代覆盖。你的输出是一个单一的值,因为这是你的代码所做的;一个输出值:'cout << array [63] [63] << endl;'。我认为你需要重新审视你正在学习的语言的部分内容,因为这里有很多缺失。 – WhozCraig
'infile >> array [63] [63];'这超出了界限并且是未定义的行为。 –