i wrote following code for writing data to a file
#include<stdio.h>
struct bank{
int accn;
char last[10];
char first[15];
double bal;
};
int main()
{
FILE *ptr;
int i;
struct bank cl={0," "," ",0.00};
printf("%d %d %d\n",sizeof(struct bank),sizeof(int),sizeof(double));
if ((ptr=fopen("banking_r.dat","wb"))==NULL)
printf("file can't be opened");
else
{
printf("enter account number,last name,first name and balance\n");
fprintf(ptr,"%s%10s%10s %s\n","account no","last name","first name","balance");
for (i=1;i<=2;i++)
{
fscanf(stdin,"%d%10s%10s%lf",&cl.accn,cl.last,cl.first,&cl.bal);
fseek(ptr,(cl.accn-1)*sizeof(struct bank),SEEK_SET);
printf("%d\n",sizeof(struct bank));
fwrite(&cl,sizeof(struct bank ),1,ptr);
fprintf(ptr,"\n");}
fclose(ptr);
}
return 0;
}
after it i entered data as
1 parker peter 23.89
2 parker arnold 23.45
then, i wrote following code to read the file
#include<stdio.h>
struct bank{
int accn;
char last[10];
char first[15];
double bal;
};
int main()
{
FILE *ptr;
struct bank cl;
if ((ptr=fopen("banking_r.dat","rb"))==NULL)
printf("file can not be opened");
else {
while(!feof(ptr)) {
fread(&cl,sizeof(struct bank),1,ptr);
//if (cl.accn!=0)
printf("%-4d %-10s %-10s %-4.2f\n",cl.accn,cl.last,cl.first,cl.bal) ;
}
}
return 0;
}
and the output i get is
1 parker peter 23.89
2 parker arnold 23.45
10 parker arnold 23.45
why did the last line get printed in output?