title: Reading File Into Struct Pointer
body: <p>My file format is :
  &quot;<code>name#totalMoney#ratio#luckyNumber&quot;</code>
  I,m trying to read that into a dynamic struct pointer.(i want to use it like array)
  I want to assign all the lines in the file to the struct array by assigning each line to an element of the struct array.My problem is when i compile this code only the first line of the file is read into struct array and nothing else.Thank for your helps..</p>
  <pre><code>#include&quot;Person.h&quot;
  #include&lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include&lt;math.h&gt;
  #define NAMSZ 50//LENGTH FOR NAME BUFFER
  
  
      void test_1(){
         People *tmp=NULL;
         int lengtPerson=pFileLength();//it returns number of line in the file
         tmp=allocate_struct_array(&amp;tmp,lengtPerson);
         readPerson(&amp;tmp);
        }
         
      void main(){    
        test_1();    
      }
      #include&quot;Person.h&quot;//it contains declaration of the functions below and
  /*typedef struct P{
       char *name;
       double totalMoney;
       double ratio;
       double luckyNumber;
  }   People; */
      People *allocate_struct_array(People **parr,int total_line){
          return *parr = malloc (total_line * sizeof **parr);
      }
      int readPerson(People **parr){
           char c;
          int MAXC=500;//max length of a line in the file
          char buf[MAXC];    
                      /* temporary array to hold each line */
          int count = 0, 
              nparr = 0,
             count_lines = 0;
          
          FILE *fileptr = fopen (&quot;Kisiler.txt&quot;, &quot;r&quot;);
          
          if (!fileptr) {     /* always validate file open for reading */
              perror (&quot;fopen-fileptr&quot;);
              
          }
         
          while (nparr &lt;= count_lines &amp;&amp; fgets (buf, MAXC, fileptr)) {
              char name[NAMSZ];       //temporary array for name 
              size_t len;             // length of name
            
              if (sscanf (buf, &quot;%[^#]#%lf#%lf#%lf&quot;,name, &amp;(parr[nparr]-&gt;totalMoney),
                          &amp;(parr[nparr]-&gt;ratio),&amp;(parr[nparr]-&gt;luckyNumber)) != 4) {
                  fputs (&quot;error: invalid line format.\n&quot;, stderr);
                  continue;
              }
              len = strlen (name);    // get length of name 
              
               parr[nparr]-&gt;name = malloc (len + 1);        // allocate for name  
              if (!parr[nparr]-&gt;name) {                    // validate allocation 
                  perror (&quot;malloc-parr[nparr].name&quot;);
                  break;
              }
              memcpy (parr[nparr]-&gt;name, &amp;name, len + 1);
              
              nparr += 1;
          }
          
          for (int i = 0; i &lt; nparr; i++) {
              printf (&quot;  %-20s %8lf %5lf %5lf\n&quot;, 
                      parr[i]-&gt;name,parr[i]-&gt;totalMoney,parr[i]-&gt;ratio,parr[i]-&gt;luckyNumber);
              //free (parr[i].name);            /* free strings when done */
          }
          //free (parr);   
          return count_lines;
      
         fclose(fileptr);
      
      
      }
  </code></pre>