我想讀取包含4X4矩陣的數據的文本文件每個元素是由一個空格分隔,它被分成4行代表矩陣。它是繼形式閱讀文件元素明智
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
現在我遇到了代碼
static const char filename[] = "file.txt";
FILE *file = fopen (filename, "r");
if (file != NULL)
{
char line [ 128 ]; /* or other suitable maximum line size */
while (fgets (line, sizeof line, file) != NULL) /* read a line */
{
fputs (line, stdout); /* write the line */
}
fclose (file);
}
else
{
perror (filename); /* why didn't the file open? */
}
return 0;
讀取該文件,但我想知道,怎樣閱讀元素方式,這樣我就可以將它們存儲在2D array