Consider an input file "abc.txt" with integer values. Write a program to Open the file and read the content to find the sum of all values in the file.
Rule to implement the problem:
The file name should be abc.txt.
Input format to the program:
Give the input as a file which contains the integer values.
Output format:
The output will be the integer which is the sum of the integers.Display the output in the console.
Sample Input file (abc.txt):
11
21
33
Implementation of the above problem:
#include <stdio.h>
int main()
{
int i, sum=0;
FILE *file;
file = (fopen("abc.txt", "r"));
if(file == NULL)
{
printf("Error!");
}
fscanf (file, "%d", &i);
sum=sum+i;
while (!feof (file))
{
fscanf (file,"%d", &i);
sum=sum+i;
}
printf("The sum of the integers in the file abc.txt is:%d",sum-6);
fclose (file);
return 0;
}
Output:
The sum of the integers in the file abc.txt is:65
The sum of the integers in the file abc.txt is:65
Thanks
Mukesh Rajput
Post A Comment:
0 comments: