title: Read data from file into structure
body: <p>I am working on a program that can process structure items in linkedlist/nodes.
  I have most of the functions running fine, however am stuck on how to read from a txt file into a structure (the readFromFile function).
  I have been reading around but am still quite confused, mainly on how to write this as a function instead of in main, and also reading into a structure</p>
  
  <p>any help would be appreciated.</p>
  
  <p>EDIT:
  I cannot seem to get the functions in the answers to work at the moment, so I am changing to trying to make the program read from txt in main.
  I could open the file, but the problem is:
  How do I read data into my linked list?</p>
  
  <p>(code has been modified)</p>
  
  <p>The text file I am reading from is formatted like this:</p>
  
  <hr>
  
  <pre><code>#1 Flat Blade Screwdriver
  12489
  36
  .65
  1.75
  #2 Flat Blade Screwdriver
  12488
  24
  .70
  1.85
  #1 Phillips Screwdriver
  12456
  27
  0.67
  1.80
  #2 Phillips Screwdriver
  12455
  17
  0.81
  2.00
  Claw Hammer
  03448
  14
  3.27
  4.89
  Tack Hammer
  03442
  9
  3.55
  5.27
  Cross Cut Saw
  07224
  6
  6.97
  8.25
  Rip Saw
  07228
  5
  6.48
  7.99
  6" Adjustable Wrench
  06526
  11
  3.21
  4.50
  </code></pre>
  
  <hr>
  
  <p>My program so far:</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;stdlib.h&gt;
  
  typedef struct inventory
  {
      char invName[36];
      int  invPartNo;
      int  invQOH;
      float invUnitCost;
      float invPrice;
  }stock;
  
  struct  NODE
  {
      union
      {
          int  nodeCounter;
          void  *dataitem;
      }item;
      struct NODE *link;
  };
  
  struct NODE *InitList();
  void DisplayNode(struct inventory *);
  struct inventory * ReadData(FILE *);
  void DisplayList(struct NODE *);
  struct NODE* GetNode(FILE *);
  void  Add2List(struct NODE *, struct NODE *);
  struct NODE* SearchList(struct NODE *, int );
  void  DeleteNode(struct NODE *, int );
  
  
  int main(int argc, char* argv[])
  {
      struct NODE *header;
      header = InitList();
  
      char ch, file_name[25];
     FILE *fp;
  
     printf("Enter the name of file you wish to see\n");
     gets(file_name);
  
     fp = fopen(file_name,"r"); // read mode
  
     if( fp == NULL )
     {
        perror("Error while opening the file.\n");
        exit(EXIT_FAILURE);
     }
  
     printf("The contents of %s file are :\n", file_name);
  
     while( ( ch = fgetc(fp) ) != EOF )
     {
        //what to put here?
     }
  
     fclose(fp);
     DisplayList(header);
  
  
  
      return 0;
  }
  
  struct NODE *InitList()
  {
      struct NODE *temp = (struct NODE*)malloc(sizeof NODE);
  
      temp-&gt;item.nodeCounter = 0;
      temp-&gt;link = NULL;
      return temp;
  }
  
  
  void  Add2List(struct NODE *start, struct NODE *NewNode)
  {
      struct NODE *current = start;
  
      while (current-&gt;link != NULL)
          current = current-&gt;link;
  
      current-&gt;link = NewNode;
      NewNode-&gt;link = NULL;
  
      start-&gt;item.nodeCounter++;
  }
  
  
  struct NODE* GetNode(FILE *fptr)
  {
      struct NODE *temp = (struct NODE*)malloc(sizeof NODE);
  
      temp-&gt;item.dataitem = ReadData(fptr);
      temp-&gt;link = NULL;
  
      return temp;
  }
  
  
  void DisplayList(struct NODE *start)
  {
      struct NODE *current = start-&gt;link;
  
      while (current != NULL)
      {
          DisplayNode((struct inventory *)current-&gt;item.dataitem);
          current = current-&gt;link;
  
      }
  }
  
  
  void DisplayNode(struct inventory *stuff)
  {
      /*
      char invName[36];
      int  invPartNo;
      int  invQOH;
      float invUnitCost;
      float invPrice;
      */
  
      printf("Name: %s", stuff-&gt;invName);
      printf("Part Number: %d", stuff-&gt;invPartNo);
      printf("Quantity on hand: %d", stuff-&gt;invQOH);
      printf("Unit Cost: %0.2f", stuff-&gt;invUnitCost);
      printf("Price %0.2f", stuff-&gt;invPrice);
  }
  
  
  struct inventory * ReadData(FILE *fptr)
  {
      struct inventory *temp = (struct inventory *)malloc(sizeof inventory);
  
      if(fptr==stdin)
          printf("Enter item name: ");
      fscanf_s(fptr, "%s", temp-&gt;invName);
      if(fptr==stdin)
          printf("Enter item part number: ");
      fscanf_s(fptr, "%d", &amp;temp-&gt;invPartNo);
      if(fptr==stdin)
          printf("Enter item quantity on hand: ");
      fscanf_s(fptr, "%d", &amp;temp-&gt;invQOH);
      if(fptr==stdin)
          printf("Enter item unit cost: ");
      fscanf_s(fptr, "%f", &amp;temp-&gt;invUnitCost);
      if(fptr==stdin)
          printf("Enter item price: ");
      fscanf_s(fptr, "%f", &amp;temp-&gt;invPrice);
  
      return temp;
  }
  
  struct NODE* SearchList(struct NODE *start, int oldData)
  {
      struct NODE* current = start;
      struct inventory * st = (struct inventory *)current-&gt;link-&gt;item.dataitem;
  
      while (st-&gt;invPartNo != oldData &amp;&amp; current != NULL)
      {
          current = current-&gt;link;
          if(current-&gt;link)
              st = (struct inventory *)current-&gt;link-&gt;item.dataitem;
      }
      return current;
  }
  
  void  DeleteNode(struct NODE *start, int oldData)
  {
      struct NODE *current, *oldNode;
  
      current = SearchList( start, oldData);
      oldNode = current-&gt;link;
      current-&gt;link = oldNode-&gt;link;
      free(oldNode);
      start-&gt;item.nodeCounter -= 1;
  }
  </code></pre>