/* Strread.c */
#include <stdio.h>  /* necessary for file I/O */
#include <stdlib.h>   /* necessary for atoi function */

int main()
{
 char user_name[25];
 char user_age[5];
 int age;
 FILE *infile;    /* declares file pointer named infile */

 infile = fopen("name_age.dat", "r");  /* open file for input */

 if (infile)  /* If no error occurred while opening file */
  {           /* input the data from the file. */
   fgets(user_name, 25, infile); /* read the name from the file */
   user_name[strlen(user_name) - 1] = '\0';
   fgets(user_age, 4, infile);   /* read the age from the file as a string */
   user_age[strlen(user_age) - 1] = '\0';
   age = atoi(user_age);
   printf("The name read from the file is %s.\n", user_name );
   printf("The age read from the file is %d.\n", age);
  }
 else          /* If error occurred, display message. */
  {
   printf("An error occurred while opening the file.\n");
  }
 fclose(infile);  /* close the input file */
 return 0;
}
