简体   繁体   中英

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. My 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 :

milenko@milenko-HP-Compaq-6830s:~$ ./n1
Segmentation fault (core dumped)

Why?

Some bugs:

  • float numbers[20][100]; is a 20 x 100 array. 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. The use of better variable names than "totalNums" (for example something like max_x and max_y ) would likely have eliminated this bug.

  • count is uninitialized before use. Enable compiler warnings.

  • You don't free() the variable line anywhere. 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**. 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).

You should try to read the line with some function like 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:

size_t count = 100;
char *line = malloc(count);

while(getline(&line, &count, myFile)!=-1) { ... }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM