forked from portfoliocourses/c-example-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_number_average.c
More file actions
49 lines (42 loc) · 1.18 KB
/
file_number_average.c
File metadata and controls
49 lines (42 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*******************************************************************************
*
* Program: Average of Numbers In a File
*
* Description: Example of finding the average of numbers in a file in C.
*
* YouTube Lesson: https://www.youtube.com/watch?v=e6MxeC7htSE
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
// size of input buffer
#define BSIZE 1024
int main()
{
FILE *fh;
char buffer[BSIZE];
double average, sum = 0;
int total = 0;
// open the file
fh = fopen("file.txt", "r");
// if the file failed to open successfully, exit with an error message
if (fh == NULL)
{
printf("Error opening file.\n");
return 1;
}
// read each line of the file until we reach the end of the file
while (fgets(buffer,BSIZE,fh) != NULL)
{
// convert the string format number at each line to a double, add it to
// the sum, and keep track of the total number of numbers
sum += atof(buffer);
total++;
}
// compute the average and print the result
average = sum / total;
printf("average: %.2f\n", average);
return 0;
}