Monday, October 1, 2018

Reading Structure from file

Following program will read a Structure (customer) data from a Customer data file.

#include <stdio.h>
#include <stdlib.h>

struct customer {
   char  fname[30],lname[30];
   int   ac_num;
   float ac_balance;
};

void main ()
{
   FILE *fp;
   struct customer input;

   fp = fopen ("customer.dat","r");
   if (fp == NULL)
   {
      printf("\nFile reading error\n\n");
      exit (1);
   }

   while (fread (&input, sizeof(struct customer), 1, fp))
      printf ("Name = %s %s,   Acct Num = %d   Balance = %8.2f\n",
              input.fname, input.lname, input.ac_num, input.ac_balance);
}

Output of Program

Name = ABC PATEL,   Acct Num = 101   Balance = 55000.00
Name = XYZ SHAH,   Acct Num = 102   Balance = 50000.00

No comments:

Post a Comment