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


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: