[英]Simple 2d array in c,Segmentation fault (core dumped)
My code 我的密码
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i=0,totalNums,totalNum,j=0;
size_t count;
float numbers[20][100];
char *line = malloc(100);
FILE *myFile;
myFile = fopen("vel.txt", "r");
if (myFile == NULL)
{
printf("Error Reading File\n");
exit (0);
}
while(getline(&line, &count, myFile)!=-1) {
for (; count > 0; count--, j++)
sscanf(line, "%f", &numbers[i][j]);
i++;
}
totalNums = 100;
totalNum = 20;
for (i=0 ; i<totalNums ; i++) {
for (j=0 ; j<totalNum ; j++) {
printf("\n%f", numbers[i][j]);
}
}
fclose(myFile);
return 0;
}
My goal is to read 2d array. 我的目标是读取2D数组。 My vel.txt 我的vel.txt
2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0
2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0
2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0 2900.0
3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0
3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0
3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0
3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0 3600.0
4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0
4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0 4700.0
4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0
5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 5100.0 5100.0 5100.0 5100.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0
5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0
5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0
5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0
5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0
5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0
5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 4900.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5100.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0
5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0
5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0
5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0 5400.0
I have compiled with gcc my code but I got : 我已经用gcc编译了我的代码,但是得到了:
milenko@milenko-HP-Compaq-6830s:~$ ./n1 Segmentation fault (core dumped)
Why? 为什么?
Some bugs: 一些错误:
float numbers[20][100];
is a 20 x 100 array. 是20 x 100的数组。 This: 这个:
totalNums = 100; totalNum = 20; for (i=0 ; i<totalNums ; i++) { for (j=0 ; j<totalNum ; j++) { printf("\\n%f", numbers[i][j]); } }
iterates over a 100 x 20 array. 迭代100 x 20数组。 The use of better variable names than "totalNums" (for example something like max_x
and max_y
) would likely have eliminated this bug. 使用比“ totalNums”更好的变量名(例如,诸如max_x
和max_y
类的东西)可能会消除此错误。
count
is uninitialized before use. count
在使用前未初始化。 Enable compiler warnings. 启用编译器警告。
You don't free()
the variable line
anywhere. 您不会在任何地方free()
变量line
。 Actually there is no reason to use dynamic allocation here at all. 实际上,这里根本没有理由使用动态分配。
The following code: 如下代码:
while(getline(&line, &count, myFile)!=-1) {
for (; count > 0; count--, j++)
sscanf(line, "%f", &numbers[i][j]);
i++;
}
Will put in count
the** number of bytes read**. 将count
**读取的字节数**。 But in next line, you consider it as the number of numbers to read ... And on number is at least composed of 2 bytes (one per character + one for the space). 但是在下一行中,您将其视为要读取的数字的数量 ...并且数字至少由2个字节组成(每个字符一个+空格一个)。
You should try to read the line with some function like strtok
. 您应该尝试使用诸如strtok
功能读取该行。 Warning, this function is not so trivial to use, read carrefully the documentation. 警告,此功能使用起来并不容易,请仔细阅读文档。
fix like this 像这样修复
size_t count=0;
float numbers[20][100];
char *line = NULL;
... ...
while(i < 20 && getline(&line, &count, myFile)!=-1) {
int len = 0, pos = 0;
j = 0;
while(j < 100 && 1 == sscanf(line + pos, "%f%n", &numbers[i][j++], &len))
pos += len;
i++;
}
free(line);
totalNums = i;
totalNum = 100;//j;
Your count
is not initialized before calling getline
, try this: 调用getline
之前,您的count
未初始化,请尝试以下操作:
size_t count = 100;
char *line = malloc(count);
while(getline(&line, &count, myFile)!=-1) { ... }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.