0

I am scanning lines from a textfile and putting elements into specific arrays, sscanf is working fine and inserting variables into arrays as well BUT printing out first element of string array result in segmentation fault(printing works for first elements from other arrays and for rest elements in string array)

Here is my code:

char **shipTable = NULL;
char *notimportant;
double *dirTable;
double *numTable;
double *speedTable;
double *latTable;
double *lngTable;

void scanShips(){
   char fname[30];
   char line[150];
   char shipname[10];
   double lat;
   double lng;
   double speed;
   double dir;
   int numofShips;
   numofShips=1;
   FILE *myfile;



   printf("give file name containing ships /n");
   scanf("%s",fname);
   myfile = fopen(fname,"rt");
   fgets(line,80,myfile);
   sscanf(line, "%d %d %d %d %d %d", &day, &month, &year, &h, &min, &sec);

   while ( fgets( line,100,myfile) != 0 ) {
       sscanf(line, "%s %lf %lf %lf %lf", shipname, &lat, &lng, &dir, &speed);
       printf("%s",shipname);
       printf("\n");

       shipTable = realloc( shipTable, numofShips*sizeof(char*) );
       latTable = realloc( latTable, numofShips*sizeof(double) );
       lngTable = realloc( lngTable, numofShips*sizeof(double) );
       dirTable = realloc( dirTable, numofShips*sizeof(double) );
       speedTable = realloc( speedTable, numofShips*sizeof(double) );
       shipTable[numofShips-1]=malloc((10)*sizeof(char));


       strcpy (shipTable[numofShips-1],shipname);
       dirTable[numofShips-1]=dir;
       speedTable[numofShips-1]=speed;
       latTable[numofShips-1]=lat;
       lngTable[numofShips-1]=lng;

       numofShips++;
   //printf("%d",numofShips);

    }
    fclose ( myfile);

//note:
printf("%s",shipTable[0]);//<---Segmentation fault
printf("%s",shipTable[1]);//<---Perfectly fine, as well as rest fo the array
printf("%f",dirTable[0]);//<---Perfectly fine, as well as rest of "double" arrays

Example file :
13 11 2011 13 04 00
GW1927 52.408 -4.117 1.000 0.000
GS452 51.750 -4.300 5.000 10.000
EI597 52.100 -6.000 90.000 12.000
EI600 52.000 -5.900 10.000 15.000
EI601 54.000 -5.900 10.000 15.000

Shipname length will never be longer than 9 chars.

Heksan
  • 3
  • 2
  • What are the contents of data file? I am suspecting that the first `shipName` is having more number of characters than 9 (==10-1). try increasing length of shipname `char shipname[50];` & `shipTable[numofShips-1]=malloc((50)*sizeof(char));`. Also, I would recommend using `calloc` instead of `malloc`. – anishsane Nov 27 '13 at 12:20
  • 3
    I suggest you check the return values from your calls to `malloc()` and `realloc()`. Also, what happens if `shipName` is assigned a string of more than 9 characters? – r3mainer Nov 27 '13 at 12:21
  • 2
    Also check return values of `fopen`, `scanf`, `sscanf` and `fgets`. – user694733 Nov 27 '13 at 12:32
  • You can create a string copy with strdup() instead of malloc_strcpy. Also, does this example file cause the crash? – egur Nov 27 '13 at 18:06

1 Answers1

0

Your program is incomplete. Once I make it complete (by adding necessary #includes, defining day, month, etc.), it compiles and works just fine.

Conclusion: you are not telling us the whole story, and the bug is in the code that you didn't show.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362