QUESTION : Consider the file expenses.txt with the following information:
22
167
33
-1
Write a code segment that opens the file, reads the file and computes the sum of all the expenses, prints the sum. Here is part of the code. Fill in the missing code so it performs the required task.
Code:
int main( void )
{
/* declarations */
int item, sum;
FILE* infile;
/* initialize sum */
/* open the file for input */
/* read the first data value */
while ( item>=0 )
{
/* process the value */
/* read the next data value */
}
/* print the final sum */
/* close the file */
return 0;
}
i made that from a different way in below. But i wanna make this question like above. I dont understand how works the while loop in this code. Thanks for your helps.
Code:
#include <stdio.h>
int main()
{
FILE *infile;
int x=22;
int y=167;
int z=33;
int a=-1;
int sum;
sum = x+y+z+a;
infile = fopen("expenses.txt","w");
fputs( "Show the sum of the numbers!\n",infile );
fprintf(infile,"%d + %d + %d+ %d = %d",x,y,z,a,sum);
fclose(infile);
puts("Saved, please continue.");
getchar();
}