i'm studying file processing. when i execute the second code that Reading Data from a Sequential Access File something goes wrong with it. i couldn't find what is wrong. is there anyone who can help me?
/*creating a sequential file*/
#include<stdio.h>
int main()
{
int account;
char name [30];
double balance;
FILE *cfPtr;
if((cfPtr=fopen("client.dat","w"))==NULL)
printf("file could not be opened\n");
else
{
printf("enter account name and balance\n");
printf("enter eof to end input \n");
printf("?");
scanf("%d%s%lf",&account,name,&balance);
while(!feof(stdin)){
fprintf(cfPtr,"%d%s%.2f\n",account,name,balance);
printf("?");
scanf("%d%s%lf",&account,name,&balance);
}
fclose(cfPtr);
}
return 0;
}
/* Reading Data from a Sequential Access File */
#include<stdio.h>
#include<conio.h>
int main()
{
int account;
char name[30];
double balance;
FILE *cfPtr;
if((cfPtr=fopen("client.dat","r"))==NULL)
printf("file could not be opened\n");
else
{
printf("%-10s%-13s%s\n","account","name","balance");
fscanf(cfPtr,"%d%s%lf",&account,name,&balance);
while(!feof(cfPtr)){
printf("%-10d%-13s%7.2f\n",account,name,balance);
fscanf(cfPtr,"%d%s%lf",&account,name,&balance);
}
fclose(cfPtr);
}
getch();
return 0;
}
when i execute first code i enter these values:
100 jones 24.98
200 doe 345.67
300 White 0.00
400 stone -42.16
500 rich 224.62
and when i execute the second code to read from the file i get this result: