Using the feof() function, on my output file i have the last value printed twice. How can i avoid that? My output should be
12 6 15 13
and i have
12 6 15 13 13
Thank you!
while(!feof(pfile1))
{
sum=0;
fscanf(pfile1,"%d%d%d",&choice,&day,&val);
if(choice==0)
{
i=day-1;
a[i]=a[i]-val;
}
else if(choice==1)
{
for(i=day-1;i<=val-1;i++)
{
sum=sum+a[i];
}
fprintf(pfile2,"%d\n",sum);
}
}
One option is to check the return value of
fscanf:So, something like:
The other option is to check for
feofafter executingfscanf. For example: