Hi,
i have a problem here i am trying to write to a file through fwrite and read through fread functions.
it asks me to enter 5 values with no problem but when i trying to read from a file it prints 6 values, the 5th values prints twice what's wrong with the code ??
here is my code:
#include<stdio.h>
int main()
{
int height,i=0;
float avg=0;
FILE *fp;
if((fp = fopen("file1","w")) != NULL)
{
while(i<5)
{
printf("Enter Height of Student %d : ",i+1);
scanf("%d",&height);
fwrite(&height,2,1,fp);
i++;
}
}
fclose(fp);
printf("\n\n");
if((fp = fopen("file1","r")) != NULL)
{
while(!feof(fp))
{
fread(&height,2,1,fp);
printf("%d\n",height);
}
}
fclose(fp);
return 0;
}