c - Not sure why I'm getting a "Bus error 10" with strtok/ EOF -


i'm trying read in every line txt document , tokenize each line specific data. however, keep getting error "bus error 10" @ end of loop. i'm not sure why happened, buffer char * has more enough allocated memory.

void* producer(void *arg) {     printf("\nentered producer\n");     int i, item, index;      index = (int)arg;      file *f = fopen(bookorders, "r");     char c = fgetc(f);     int z =0;     while (c!=eof) {         char * buffer = (char *)calloc(1000, 1);         while (c!='\n') {             *(buffer+z) = c;             z++;             c = fgetc(f);         }         char * rpr;          char delim[2] = "|";          char * title = strtok_r(buffer, delim, &rpr);         title = title +1;         int leng = (int)strlen(title);         *(title+leng-1) = '\0';          double cost = atof(strtok_r(null, delim, &rpr));          int id = atoi(strtok_r(null, delim, &rpr));          char * cat_name = strtok_r(null, delim, &rpr);          c = fgetc(f);         printf("%c\n", c);         z = 0;         *buffer = '\0';          free(buffer);     }  fclose(f); return null; } 

i omitted code on mutexes because skeleton doesn't work begin with; figured add pertinent code. error have eof condition of while loop?

there large number of things in code running off end of buffer or dereferencing null pointer (which might cause bus error). it's going shorter me rewrite code list mistakes, here goes:

char *buffer = malloc(1000); if ( !buffer )     return null;  file *f = fopen(bookorders, "r"); int c, z;  if ( f ) (;;) {     ( z = 0; z < 999 && (c = fgetc(f)) != eof && c != '\n'; ++z )         buffer[z] = c;      if ( z == 0 && c == eof )         break;      buffer[z] = 0;      char const delim[] = "|";     char *rpr;     char *title = strtok_r(buffer, delim, &rpr);     if ( title[0] )     {         ++title;         title[strlen(title) - 1] = 0;     }  // better use `strtod` , `strtol` here     char *ptr = strtok_r(null, delim, &rpr);     double cost = ptr ? atof(ptr) : 0;      ptr = strtok_r(null, delim, &rpr);     int id = ptr ? atoi(ptr) : 0;      char * cat_name = strtok_r(null, delim, &rpr);  // if want title, cost, id, cat_name, now's chance }  free(buffer); fclose(f); return null; 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -