title: C, reading from file into structure
body: <p>I've been struggling with this for days and I can't figure out why it doesn't work.</p>
  
  <p>I'm trying to read numbers from file with numbers written like this:</p>
  
  <pre><code>0 2012 1 1 2000.000000
  0 2012 1 1 3000.000000
  1 2012 1 1 4500.000000
  </code></pre>
  
  <p>my structure:</p>
  
  <pre><code>struct element{
  
          int id;
          int sign;
          int year;
          int month;
          double amount;
  
          struct element *next;
  
  
  };
  
  struct queue{
      struct element *head;
      struct element *tail;
      struct element *head2; 
      struct element *temp;  
      struct element *temph; 
  
      int size;
  };
  </code></pre>
  
  <blockquote>
    <p>(head2, temp and temph are used in sorting structure)</p>
  </blockquote>
  
  <p>and reading from a file:</p>
  
  <pre><code>void read_str(struct queue *queue){
  
      FILE *reads;
  
      char filename[40];
      int temp;
  
      printf("Type in name of the file\n");
      scanf("%s",&amp;filename);
      reads=fopen(filename, "r");
      if (reads==NULL) {
          perror("Error");
          return 1;
      }
      else { 
          while(!feof(reads)) {
              struct element *n= (struct element*)malloc(sizeof(struct element));             
              fscanf(reads,"%d %d %d %d %lf", n-&gt;id, n-&gt;sign, n-&gt;year, n-&gt;month, n-&gt;amount);                  
              n-&gt;next=NULL;                   
  
              if(queue-&gt;head ==NULL) {
                  queue-&gt;head=n;
              }
              else {
                  queue-&gt;tail-&gt;next=n;
              }
  
              queue-&gt;tail=n;
              queue-&gt;size++;                  
  
          }           
      }
  }
  </code></pre>
  
  <p>I can change the way the data looks in a file by changing the function that writes it, but I don't think that's the problem. My guess I'm using <code>malloc</code> in a wrong way.</p>