Reading Records from File

Let us now write a program that reads the employee records created by the above program. Here is how it can be done…

/* Read records from a file using structure */
#include "stdio.h"
int main() 
{
	FILE *fp;
	struct emp 
	{
		char name[40];
		int age;
		float bs;
	};
	struct emp e;
	fp = fopen("targetfile.txt", "r");
	if (fp == NULL) 
	{
		puts("Cannot open file");
		exit(0);
	}
	while (fscanf(fp, "%s %d %f", e.name, &e.age, &e.bs) != EOF)
		printf("\n%s %d %f", e.name, e.age, e.bs);
	fclose(fp);
	getchar();
	return 0;
}