C 将数据文件读入2D数组后出现分段错误
当我试图运行代码将数据文件读入2D数组时,我遇到了一个分段错误。不确定是没有正确地传递数组,还是将文件正确地读入数组C 将数据文件读入2D数组后出现分段错误,c,multidimensional-array,io,segmentation-fault,C,Multidimensional Array,Io,Segmentation Fault,当我试图运行代码将数据文件读入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(&l
#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);
}
像这样改变:
#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;
}
#包括
#包括
无效两次输入(文件*fp,int arr[][10],int*r,int*c){
大小\u t计数=0;
char*line=NULL;
*r=*c=0;
while(getline(&line,&count,fp)!=-1){
char*readPos=行;
int readLen=0,col=0;
而(列<10&&1==sscanf(readPos、%d%n、&arr[*r][col]、&readLen)){
readPos+=readLen;
++上校;
}
*c=col;//必须使用相同数量的列
++*r、 //或(*r)++;
}
自由线;
}
内部主(空){
int a,b,i,j;
整数数组[10][10];
文件*f;
f=fopen(“试验”,“r”);
两个D_输入(f、数组、i和j);
对于(a=0;a
更改如下:
#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;
}
#包括
#包括
无效两次输入(文件*fp,int arr[][10],int*r,int*c){
大小\u t计数=0;
char*line=NULL;
*r=*c=0;
while(getline(&line,&count,fp)!=-1){
char*readPos=行;
int readLen=0,col=0;
而(列<10&&1==sscanf(readPos、%d%n、&arr[*r][col]、&readLen)){
readPos+=readLen;
++上校;
}
*c=col;//必须使用相同数量的列
++*r、 //或(*r)++;
}
自由线;
}
内部主(空){
int a,b,i,j;
整数数组[10][10];
文件*f;
f=fopen(“试验”,“r”);
两个D_输入(f、数组、i和j);
对于(a=0;a
花了一点时间才明白您要做什么。如果我理解正确,您需要声明一个大小为mxn
的静态数组,然后让两个du输入
填充到mxn
元素,返回通过指针r
和c
填充的实际行和列值
这样做没有错,但是您必须强制读取(或初始化)每行相同数量的列,以便读取的列数的值不会最终指向未初始化的值。您还必须防止读取超出数组限制
虽然在将每行数据解析为整数值之前,您可以使用getline
读取每行数据,但在two\u input
中使用静态声明的缓冲区和fgets
非常简单。(它还消除了释放由getline
分配的内存的需要)
将所有部分放在一起,您可以编写类似于以下内容的two\u D\u 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;
}
}
#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
$./bin/a2dfscanf花了一点时间才明白您要做什么。如果我理解正确,您需要声明一个大小为mxn
的静态数组,然后让两个du输入
填充到mxn
元素,返回通过指针r
和c
填充的实际行和列值
这样做没有错,但是您必须强制读取(或初始化)每行相同数量的列,以便读取的列数的值不会最终指向未初始化的值。您还必须防止读取超出数组限制
虽然在将每行数据解析为整数值之前,您可以使用getline
读取每行数据,但在two\u input
中使用静态声明的缓冲区和fgets
非常简单。(它还消除了释放由getline
分配的内存的需要)
将所有部分放在一起,您可以编写类似于以下内容的two\u D\u 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;
}
}
#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
$./bin/a2dfscanf您的代码到底在哪里失败?使用fscanf
直接从文件中提取数字不是更容易吗?函数中的int**arr
与int*数组[10][10]不兼容
在调用者中。另外,您在初始化r
和c
的方式上有问题。另外,我不认为getline
返回的计数是您想要循环的。我突然想到,您没有释放two\u input
中分配的内存。您的代码到底在哪里弱点?使用fscanf
直接从文件中提取数字不是更容易吗?函数中的int**arr
与int*数组[10][10]不兼容
在调用者中。而且你在初始化r
和c
的方式上有问题。而且我不认为getline
返回的计数是你想要循环的。我只是突然想到你没有释放两个\u输入中分配的内存。
。真可怕,它们是太接近了。我忘了检查行数。是的,但看到所有其他相似之处很有趣。我有一个更早的约会,然后稍微尝试了一下答案,然后我们两人在一分钟内互相发布了帖子。除了时间安排,我想在C语言中确实有一些标准的处理方法,因为解决方案的关键几乎是相同的。可怕的是,这些方法非常接近。我忘记了检查行数。是的,但是看到所有其他的相似之处很有趣。我之前有个约会,后来才开始涉足