当我尝试运行代码将数据文件读入2D数组时,我遇到了分段错误。不确定我是不是正确传递数组还是正确地将文件读入数组。
#include <stdio.h>
#include <stdlib.h>
void two_D_input(FILE *fp, int **arr, int* r, int* c)
{
size_t count;
char *line = malloc(20);
while(getline(&line, &count, fp) != 1) {
for(; count > 0; count--, *c++)
sscanf(line, "%d", *&arr[*r][*c]);
*r++;
}
}
main()
{
int a, b, i, j;
int* array[10][10];
FILE *f;
f = fopen("test", "r");
two_D_input(f, &array[0][0], &i , &j);
for(a =0; a < i; a++){
for(b=0;b<j; b++){
printf("\n%d", array[a][b]);
}
}
fclose(f);
}
目前文件测试看起来像这样。
1 0
1 1
答案 0 :(得分:2)
花了一点时间才明白你要做什么。如果我理解正确,您希望声明一个M x N
大小的静态数组,然后让two_D_input
填充M x N
个元素,返回实际的行和<通过指针r
和c
填充em> col 值。
以这种方式执行它没有任何问题,但是您必须强制读取(或初始化)每行相同数量的列,以便读取的列数的值不会最终指向未初始化的值。您还必须防止读数超出阵列的限制。
虽然在将每行数据解析为整数值之前,您可以使用getline
读取每行数据,但在fgets
中使用静态声明的缓冲区和two_D_input
只是一件容易的事。 。 (它还消除了free
getline
内存的需要
将所有部分组合在一起,您可以为您写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
仔细看看,如果您有任何问题,请告诉我。
答案 1 :(得分:0)
改变如下:
#include <stdio.h>
#include <stdlib.h>
void two_D_input(FILE *fp, int arr[][10], int *r, int *c){
size_t count = 0;
char *line = NULL;
*r = *c = 0;
while(getline(&line, &count, fp) != -1) {
char *readPos = line;
int readLen = 0, col = 0;
while(col < 10 && 1==sscanf(readPos, "%d%n", &arr[*r][col], &readLen)){
readPos += readLen;
++col;
}
*c = col;//It is necessary all the same number of columns
++*r;//or (*r)++;
}
free(line);
}
int main(void){
int a, b, i, j;
int array[10][10];
FILE *f;
f = fopen("test", "r");
two_D_input(f, array, &i , &j);
for(a = 0; a < i; a++){
for(b = 0; b < j; b++){
printf("%d ", array[a][b]);
}
putchar('\n');
}
fclose(f);
return 0;
}