花了一点时间来理解您要做什么。如果我理解正确,你想声明一个M x N 大小的静态数组,然后让two_D_input 填充到M x N 元素返回实际的 row 和 col 通过指针r 和c 填充的值。
这样做并没有错,但是您必须强制每行读取(或初始化)相同数量的列,以便读取的列数的值最终不会指向未初始化的值.您还必须防止读取超出数组的限制。
虽然您可以在将每行数据解析为整数值之前使用getline 读取每一行数据,但在two_D_input 中使用静态声明的缓冲区和fgets 只是一个简单的方法。 (也省去了freegetline分配的内存)
将所有部分放在一起,您可以编写类似于以下内容的two_D_input函数:
enum { NROW = 10, NCOL = 10, MAXC = 256 };
...
void two_D_input (FILE *fp, int (*arr)[NCOL], int *r, int *c)
{
char buf[MAXC] = ""; /* temp buffer to hold line */
int tmp = 0, tmpc = 0; /* temp int and column val */
*r = *c = 0; /* initialize row, col ptrs */
while (fgets (buf, MAXC, fp)) { /* read line into buf */
char *p = buf;
int n = 0; /* read int into tmp, get offset in buf */
while (tmpc < NCOL && sscanf (p, " %d%n", &tmp, &n) == 1)
{ /* while cols < NCOL & value read from buf */
arr[*r][tmpc++] = tmp; /* assign array value */
if (tmpc > *c) *c = tmpc; /* update colum width */
p += n; /* update p for next read */
}
if (*c != tmpc) {
fprintf (stderr, "error: invalid column count.\n");
exit (EXIT_FAILURE);
}
(*r)++, tmpc = 0; /* increment row, reset col */
if (*r == NROW) /* check against max row val */
break;
}
}
(别忘了(*r)++增加r指向的值,*r++增加指针r。
将它们放在一起,您可以执行以下操作:
#include <stdio.h>
#include <stdlib.h> /* for exit */
enum { NROW = 10, NCOL = 10, MAXC = 256 };
void two_D_input (FILE *fp, int (*arr)[NCOL], int *r, int *c)
{
char buf[MAXC] = ""; /* temp buffer to hold line */
int tmp = 0, tmpc = 0; /* temp int and column val */
*r = *c = 0; /* initialize row, col ptrs */
while (fgets (buf, MAXC, fp)) { /* read line into buf */
char *p = buf;
int n = 0; /* read int into tmp, get offset in buf */
while (tmpc < NCOL && sscanf (p, " %d%n", &tmp, &n) == 1)
{ /* while cols < NCOL & value read from buf */
arr[*r][tmpc++] = tmp; /* assign array value */
if (tmpc > *c) *c = tmpc; /* update colum width */
p += n; /* update p for next read */
}
if (*c != tmpc) {
fprintf (stderr, "error: invalid column count.\n");
exit (EXIT_FAILURE);
}
(*r)++, tmpc = 0; /* increment row, reset col */
if (*r == NROW) /* check against max row val */
break;
}
}
int main (int argc, char **argv)
{
int array[NROW][NCOL] = {{0}};
int i, j, r = 0, c = 0;
FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;
if (!fp) {
fprintf (stderr, "error: file open failed '%s'.\n", argv[1]);
return 1;
}
two_D_input (fp, array, &r, &c);
printf ("\n read (%d x %d) array\n\n", r, c);
if (fp != stdin)
fclose (fp);
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) printf (" %2d", array[i][j]);
putchar ('\n');
}
return 0;
}
输入文件示例
$ cat dat/a2d.txt
1 0
1 1
$ cat ../dat/10by10.txt
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49
50 51 52 53 54 55 56 57 58 59
60 61 62 63 64 65 66 67 68 69
70 71 72 73 74 75 76 77 78 79
80 81 82 83 84 85 86 87 88 89
90 91 92 93 94 95 96 97 98 99
使用/输出示例
$ ./bin/a2dfscanf <dat/a2d.txt
read (2 x 2) array
1 0
1 1
$ ./bin/a2dfscanf <../dat/10by10.txt
read (10 x 10) array
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49
50 51 52 53 54 55 56 57 58 59
60 61 62 63 64 65 66 67 68 69
70 71 72 73 74 75 76 77 78 79
80 81 82 83 84 85 86 87 88 89
90 91 92 93 94 95 96 97 98 99
查看一下,如果您有任何问题,请告诉我。