title: Reading data from text file to struct array
body: <p>What is wrong with the following code? How can I correct it?</p>
  
  <p>Data is in rows seperated by commas: name, address, idnumber, age.</p>
  
  <p>What I want is to read from a text file the above four fields, separated by commas in one line, and to store those data in the array struct given below in code as a instance of <code>struct person</code>:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  // struct person with 4 fields
      struct person
      {
          char name[100];
          char address[100];
          char IDnumber[20];
          int  age;
      };
  
  int main()
  {
     FILE *file = fopen ("personout.txt","r");
     // declares an struct array to store data
     struct person student[10];
  
     int k=0;
  
     if ( file != NULL )    
     {
         char line [ 128 ]; /* or other suitable maximum line size */
         while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
         {
             // Returns first token
             char* token = strtok(line,",");
  
            // Keep printing tokens while one of the
            // delimiters present in str[].
            while (token != NULL) {
                k=0;
             // countword++;
                printf("%s\n", token);
  
                switch (k) {
                case '1':
                    student[k].name==token;
                case '2':
                    student[k].address==token;
                case '3':
                    student[k].IDnumber==token;
                case'4':
                    student[k].age==token;
                default:
                    break;
                }
  
                k++;
  
                // put the values to array
                token = strtok(NULL, ",");
            }
        }
        printf("%s\n", student[k].name);
        fclose ( file );
    }
  
  }
  </code></pre>