0

Im trying to read a file a then insert it in a linked list but i'm having trouble to use sscanf. I dont know why but it never goes through the first if with sscanf.The file is a result from the user input and some users have more information than others so i need to differentiate the way a read each line.

The file is exactly like this.

201263066#1,2,3#9#1,2,3,4,5,6,7,8,10,11,
201263065#0,0,0#0#
201263064#0,0,0#0#
201363524#4,5,7#2#2,3,4
201596874#1,9,8#8#2,8,9,6,5,2,1
typedef struct user{
  int id;
  int city[3];
  int *places;
  int hot;
}User;

typedef struct Users_node *List_users;
typedef struct Users_node{
  User user;
  List_users next;
}Each_user;


void read_file(List_users u){
  FILE *p;

  int i=0,a,n=0;
  char line[1024];
  List_users aux2;
  char city_ids[20];
  char *pt,*pt2;
  int hotspot;
  int user_id;
  char places_id[200];

  p = fopen("userPref.txt", "r");

  while(!feof(p)){
      fgets(line,sizeof(line),p);
      if(sscanf(line,"%d#%s#%d#%s",&user_id,city_ids,&hotspot,places_id) == 4){
          aux2->user.id  = user_id;

          pt = strtok(city_ids,",");
          while(pt != NULL) {
              a = atoi(pt);
              printf("%d\n", a);
              aux2->user.city[n++] = a;
              pt = strtok(NULL, ",");
          }
          aux->user.hot = hotspot;

          pt2 = strtok(places_id,",");
          while(pt2 != NULL) {
              a = atoi(pt);
              printf("%d\n", a);
              aux2->user.places[n++] = a;
              pt2 = strtok(NULL, ",");
          }
      }else{
          aux2->user.id = atoi(strtok(line,"#"));
          aux2->user.city[0] = atoi(strtok(NULL,","));
          aux2->user.city[1] = atoi(strtok(NULL,","));
          aux2->user.city[2] = atoi(strtok(NULL,","));
          aux->user.hot = atoi(strtok(NULL,"#"));
      }
  }

  fclose(p);
}
Ken White
  • 123,280
  • 14
  • 225
  • 444
joanne
  • 1
  • 2
  • 2
    Not related to you problem, but you're using `feof` incorrectly: https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong – William Pursell May 24 '19 at 23:43
  • 1
    Very much related to your problem: what is scanf returning? It isn't 4. Understand why not and you're problem is solved. – William Pursell May 24 '19 at 23:43
  • hint: `1,2,3#9#1,2,3,4,5,6,7,8,10,11,` matches `%s` – William Pursell May 24 '19 at 23:44
  • Change `%s` into `%[^#]`, that way you will read up until `#`. – KamilCuk May 25 '19 at 00:36
  • Thanks a lot! I already solved my problem turns out it was reading what is wasn't supposed to. %[^#] worked! – joanne May 25 '19 at 00:57
  • 1
    i've rolled back your edit. It is not appropriate to add SOLVED to the title or edit a solution into the question. If you have found a solution and want to share it, do so by writing an answer in the space below that is provided for that purpose; see [Can I answer my own question?](http://stackoverflow.com/help/self-answer). Otherwise, you have two options: 1) Leave the question as-is and wait for someone to write an answer; or 2) delete the question using the link below the tags. Taking the [tour] and reading the [help] pages to learn how the site works will help your experiences here. – Ken White May 25 '19 at 01:17

0 Answers0