0
我寫這段代碼來讀取一個txt文件到一個二維數組中,它的工作正常,但在我的輸入文本文件中我有一個8 * 8的矩陣輸出給我看, 37周的cols在二維數組解決方案中讀取txt文件
#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
和輸出圖像爲: 有人能幫助嗎?
37 = 8 * 8 + 1 !!!!!!!噢,我的猜測 – Butterflay