【发布时间】:2014-03-05 08:27:34
【问题描述】:
我试图弄清楚如何从一个文件中读取两个矩阵,然后将它们分配给两个不同的二维数组。矩阵由文本文件中的新行分隔。我设法让第一个成功阅读,但现在我不知道如何阅读第二个。在我看来,必须考虑和检查分隔两个矩阵的线。如何让程序读取第二个矩阵并将其分配给数组?
矩阵和数组都是 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 类,在网上搜索“C++ FAQ Matrix”。
标签: c++ file-io matrix multidimensional-array