what is worng now????????????
no error when i run the program but it is give me double the last one
the out put is
Enter the id :1
Enter the gpa :1.2
Enter the id :2
Enter the gpa :2.6
Enter the id :3
Enter the gpa :3.8
The id is: 1 the gpa is: 1.20
The id is: 2 the gpa is: 2.60
The id is: 3 the gpa is: 3.80
The id is: 3 the gpa is: 3.80 ///// why print again?????
Press any key to continue
----
the code
Code:
#include<stdio.h>
/*********** Data ***********/
typedef struct {
int id;
float gpa;
}data;
/*********** The functions ***********/
void read(data*);
void print(FILE*);
/*********** Main ***********/
int main(){
data R;
FILE *f;
int i;
if((f=fopen("f.dat","w"))==NULL)
printf("sorry i can not open the file\n");
else{
for(i=0;i<3;i++){
read(&R);
fprintf(f,"%d %.2f ",R.id,R.gpa);
}
fclose(f);
if((f=fopen("f.dat","r"))==NULL)
printf("sorry i can not open the file\n");
else{
while(!feof(f)){
fscanf(f,"%d %f",&R.id,&R.gpa);
printf("The id is: %d the gpa is: %.2f\n",R.id,R.gpa);
}
fclose(f);
}
}
return 0;
}
/*********** Read ***********/
void read(data *R){
printf("Enter the id :");
scanf("%d",&R->id);
printf("\n");
printf("Enter the gpa :");
scanf("%f",&R->gpa);
printf("\n");
}
/*********** Print ***********/
void print(FILE *f1){
data R;
if((f1=fopen("f1.dat","r"))==NULL)
printf("sorry i can not open the file\n");
else{
fscanf(f1,"%d %f",&R.id,&R.gpa);
while(!feof(f1)){
printf("The id is: %d the gpa is: %.2f\n",R.id,R.gpa);
fscanf(f1,"%d %f ",&R.id,&R.gpa);
}
fclose(f1);
}
}
/*********** End ***********/