title: Whats wrong with fread in this program?
body: <p>I'm intermediate student of C. I'm trying to make a bank management program but first I need to make a login program, so I created one of the following. As I've recently learned about file I/O in C and don't know much about fread and fwrite. I have a file (data.txt) which format if as following.</p>
  
  <p>user1   1124</p>
  
  <p>user2   3215</p>
  
  <p>user3   5431</p>
  
  <p>In the following program I've asked user to input user name and pin(4-digit password) and copy file data into a structure then compare these two for verifying information.</p>
  
  <p>What is wrong with my program and how to make fread work properly. And is the formating in data.txt file all right or should I change it.</p>
  
  <p>Thanks in advance...</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;ctype.h&gt;
  #include&lt;string.h&gt;
  
  struct user_account    {
      char u_name[30];
      int u_pin;
  } log_in;
  
  
  
      int login()
  {
      int start;
      int i, n;
      int t_pin[4];       // TEMPORARY INT PIN for storing pin inputed by user
      char t_name[30];    //  TEMPORARY STRING for storing name inputed by user
  
      FILE *fp;
      fp = fopen("data.txt","rb");        // Opening record file
  
      if(fp == NULL)
      {
      puts("Unable to open file!");
      return 1;
      }
  
      start :  {
          printf("User Name : ");
          scanf("%s",&amp;t_name);
          printf("Pin Code  : ");
  
          for(i = 0; i &lt; 4; i++)  {       // This loop is for hiding input pin
              n = getch();
  
              if(isdigit(n))  {
                  t_pin[i] = n;
                  printf("*");    }
              else    {
                  printf("\b");
                  i--;
              }
          }
  
          fread(&amp;log_in,sizeof(log_in),1,fp);
  
          // Comparing user name and pin with info in the structure copied from the file
  
          if(strcmp(log_in.u_name, t_name) == 0 &amp;&amp; log_in.u_pin == t_pin)
              puts("Login successful! Welcome User");
          else    {
              printf("\nIncorrect Information!\n");
              printf("Press any key to log in again...");
              getch();
              system("cls");
              goto start; }
          }
      }
  
      int main()
      {
      int login();
          return 0;
      }
  </code></pre>