title: How to read multiple data types from file in c
body: <p>I have to read a file that contains a list of groceries, each line has 3 parts: category, name, price</p>
  
  <p>So this is what the file looks like:</p>
  
  <pre><code>Fruit,Apple,15.6
  Vegetable,Potato,20.0
  Fruit,Banana,17.0
  Vegetable,Lettuce,13.5
  </code></pre>
  
  <p>I'm having trouble with reading those values and putting them into a linked list. </p>
  
  <p>I believe that the only problem I have is with reading them correctly and loading them into a linked list. I've tried many different combinations with <em>fscanf</em>, but none seem to work.</p>
  
  <pre><code>        typedef struct Grocery Grocery;
  
          struct Grocery
          {
              char category[100];
              char name[100];
              float price;
              Grocery *next;
          };
  
          Grocery *addNew(char *category, char *name, float price)
          {
              Grocery *newN = (Grocery*)malloc(sizeof(Grocery));
  
              strcpy(newN-&gt;category, category);
              strcpy(newN-&gt;name, name);
              newN-&gt;price = price;
              newN-&gt;next = NULL;
  
              return newN;
          }
  
          Grocery *add_on_beginning(Grocery *head, Grocery *newN)
          {
              newN-&gt;next = head;
  
              return newN;
          }
  
          Grocery *load_file(Grocery *head)
          {
              char filename[100];
  
              scanf("%s", filename);
  
              FILE *fp = fopen(filename, "r");
  
              char *category;
              char *name;
              float price;
  
              if(fp == NULL)
              {
                  printf("Error in loading file.\n");
                  return 0;
              }
              else
              {
                  while(!feof(fp))
                  {
                      fscanf(fp, "%s,%s,%lf\n", category, name, &amp;price);
  
                      Grocery *newN = addNew(category, name, price);
  
                      head = add_on_beginning(head, newN);
                  }
              }
  
              fclose(fp);
  
              printf("File loaded successfully.\n");
  
              return head;
          }
  </code></pre>
  
  <p>Whenever I debug the code, I get random numbers and letters after every element is printed, I get this:</p>
  
  <blockquote>
    <p>ï&amp;╪5Ç@ 0.000000</p>
  </blockquote>
  
  <p><a href="https://i.stack.imgur.com/LsnDe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LsnDe.png" alt="enter image description here"></a></p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Thanks for all the help, I've made it work with this <code>while(fscanf(fp, "%[^,],%[^,],%f\n", category, name, &amp;price) == 3)</code>, can someone explain what it does?</p>