我想弄清楚如何从一个文件中读取两个矩阵,然后将它们分配给两个不同的二维数组。矩阵在文本文件中以新行分隔。我设法让第一个阅读成功,但现在我不知道如何阅读第二个。我看到它的方式,必须考虑并检查分离这两个矩阵的线。如何让程序读取第二个矩阵并将其分配给数组?两个二维数组,两个矩阵,一个文件
矩阵和数组都是4x4。
这是文本文件的样子:
3 4 5 7
5 16 7 12
11 12 3 9
9 8 1 12
15 4 3 6
1 12 3 12
7 8 19 9
11 12 8 5
这里是我的正常工作的第一阵列码。
for (int y = 0; y < 4; ++y)
{
for (int x = 0; x < 4; ++x)
{
infile >> array1[x][y];
}
}
这里是我的代码我想不通的数组。
for (int y = 0; y < 4; ++y)
{
for (int x = 0; x < 4; ++x)
{
if(x == '\n' && y == '\n') //My attempt.
{
infile >> array2[x][y];
}
}
}
这里是输出:
This will check to make sure numbers in file are written to the first array.
3 4 5 7
5 16 7 12
11 12 3 9
9 8 1 12
This will check to make sure numbers in file are written to the second array.
9.21742e-314 1.0572e-307 7.29112e-304 3.87184e-306
1.06498e-307 1.65425e-317 6.79039e-313 4.22748e+266
5.92879e-323 1.06196e-307 6.95089e-308 9.88799e-315
1.79648e-313 8.48798e-314 6.95224e-308 1.06193e-307
Press any key to continue . . .
任何帮助表示赞赏!
要读取矩阵,请在StackOverflow中搜索“C++读取文件解析矩阵” –
对于Matrix类,请在Web上搜索“C++常见问题解答矩阵”。 –