我编写此代码以将 txt 文件读入 2D 数组,它工作正常,但在我的输入 txt 中,我在输出中有一个 8*8 矩阵,向我显示我有 37 列
#include <stdio.h>
int main()
{
int c, i, j, row, col, nl, cr;
row = col = nl = cr = 0;
FILE *fp = fopen("image.txt", "r");
// Figure out how many rows and columns the text file has
while ((c = getc(fp)) != EOF)
{
if (c == '\n')
nl++;
if (c == '\r')
cr++;
col++;
if (c == '\n')
row++;
putchar(c);
}
col = (col - (nl + cr));
col = (int) (col/row);
printf("\nnumber of rows is %d\n", row);
printf("number of columns is %d\n\n", col);
return 0;
}
我的输入正常:
255 50 9 50 1 50 50 1
50 255 50 50 50 50 50 50
50 50 255 50 50 50 50 50
8 50 50 255 50 50 50 50
50 50 50 50 255 50 50 50
50 50 50 50 50 255 50 50
1 50 50 50 50 50 255 50
2 50 50 50 50 50 50 255
输出图像是:
有人可以帮忙吗?