我一直想在一个txt文件中的矩阵输入存储阵列中,但它告诉我这一点: 这是代码从文本文件中读取和二维数组存储
#include <stdio.h>
int main()
{
int c, i, j, row, col, nl, cr;
row = col = nl = cr = 0;
FILE *fp = fopen("g.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);
// read letters into array
char array[row][col];
if (fp)
{
for (;;)
{
c = getc(fp);
if (c == EOF)
{
break;
}
if (c != '\n' && c != '\r')
{
array[i][j] = c;
if (++j >= col)
{
j = 0;
if (++i >= row)
{
break;
}
}
}
}
fclose(fp);
}
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
putchar(array[i][j]);
}
putchar('\n');
}
return 0;
}
有人为请有任何想法吗? 为例TXT文件:
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
我的计划告诉我这一点:
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 $■(1gÍuáþ09■ ı¤ıu"ÒávD ê$[
► ð²( ♥ l ► ■
ê$[ ♥ l ²(O»ƒv[ 4■(Qõá
v♥ #õáv┬²║Oÿ|®v ñ|®ve┬ív
■( x■(ÿ|®v Ó²⌂ @■( áƒv╚♀[ L
■(w¯ƒv‼ ê■(I┴ávÿ|®v↓┴áv~²║O
Ó²⌂ \■(■ ─ (e┬ívÍ┬29►☺
输入文件显示它的好,但问题是阵列输出,我不undrestand为什么告诉我这个caracters
其中是您的代码中的二维数组? –
在一边注意''double atof(const char * str);'是不需要的,因为函数已经在stdlib.h中声明了。您的代码实际上不会使用我的编译器中的该行进行编译。 – jpw
双数[100] [100]; – Butterflay