Ivickt
Ivickt

Reputation: 29

Segmentation Fault when My Code Executes the printf() in c

below I have posted my code. When I compile I receive no errors, and only one warning about variables I haven't used yet. the code works all the way to the line in code where it starts to print. I have tested all the sections and I believe that one is at fault. please let me know what I am doing wrong so I can fix it.

  #include <stdio.h>
  #include <string.h>
 
  #define NUM_LINES 37
  #define LINE_LENGTH 60
 
  void select_sort_str(char list[NUM_LINES][LINE_LENGTH], int n);
  int alpha_first(char list[NUM_LINES][LINE_LENGTH], int min_sub, int max_sub);
 
 
  int main (void){
  //store each line in an array of strings
      FILE *inp;
      FILE *outp;
      char hurr[NUM_LINES][LINE_LENGTH];
      ;
 
      inp = fopen("hurricanes.csv","r");
      outp = fopen("out.txt","w");
 
 
  //read in lines from file
      for (int i = 0; i<NUM_LINES; i++){
          fgets(hurr[i], LINE_LENGTH, inp);
      }
      inp = fopen("hurricanes.cvs","r");
 
      //printf("%s", hurr[0]);
  //define function
 
 

 
      select_sort_str(hurr, NUM_LINES);
 
 
  return(0);
  }
 
 
  int
  alpha_first(char list[NUM_LINES][LINE_LENGTH],       // input - array of pointers to strings
              int min_sub,        // input - min and max subscripts of
              int max_sub)        //    portion of list to consider
  {
      int first, i;
 
      first = min_sub;
      for (i = min_sub + 1; i <= max_sub; ++i) {
          if (strcmp(list[i], list[first]) < 0) {
              first = i;
          }
      }
      return (first);
  }
 
  /*
   * Orders the pointers in an array list so they access strings in
   * alphabetical order
   * Pre: first n elements of list reference string of uniform case;
   *      n >= 0
   */
  void
  select_sort_str(char list[NUM_LINES][LINE_LENGTH],   // input/output - array of pointers being
                                  // ordered to acces strings alphabetically
                  int n)          // input - number of elements to sort
  {
      int fill,           // index of element to contain next string in order
          index_of_min;   // index of next string in order
      char *temp;
      char temp1[NUM_LINES][LINE_LENGTH];
 
      for (fill = 0; fill < n - 1; ++fill) {
          index_of_min = alpha_first(list, fill, n - 1);
 
          if (index_of_min != fill) {
              temp = list[index_of_min];
              list[index_of_min][LINE_LENGTH] = list[fill][LINE_LENGTH];
              strncpy(temp1[index_of_min], list[index_of_min], LINE_LENGTH);
              temp1[fill][LINE_LENGTH] = *temp;
 
 
          }
      }
      char *name;
      char *cat = 0;
      char *date;
 
    for (int i = 0; i<NUM_LINES; i++){
          name = strtok(NULL, ",");
          cat  = strtok(NULL, "h");
          date = strtok(NULL, " ");
      printf("%s %s %s\n", name, cat, date);
      }
 
     // for( int i =0; i<NUM_LINES; i++){
     //     printf("%s", list[i]);
     // }
  }
 

Upvotes: 0

Views: 62

Answers (1)

David Schwartz
David Schwartz

Reputation: 182885

The only first parameter you ever pass to strtok is NULL. You never actually give it anything to parse. Did you perhaps mean strtok(temp1[i], ",");?

Also, why no error checking? It's much easier to find bugs in code with error checking.

Upvotes: 4

Related Questions