Write a C program to read a set of scores from a text file, count the number of
ID: 3818019 • Letter: W
Question
Write a C program to read a set of scores from a text file, count the number of scores over 80, copy the scores over 80 into a new file, save it as 'OUTPUTFILE.DAT', and finally, print the list of number of scores over 80 on screen. Test your program with an input file (name it as 'INPUTFILE.DAT') containing the following values: 100 50 20 82 60 30 88 78 90 100 63 55 30 85 93 96 63 A sample program execution and output is depicted in Figure 5. Start of Program **** Printing scores over 80 on screen ***** 100 82 88 90 100 85 93 96 There were 8 scores over 80. End of ProgramExplanation / Answer
code:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
fp = fopen("input.txt", "r");
char buff[255];
char *line;
size_t len = 0;
ssize_t read;
FILE *fptr;
fptr = fopen("OUTPUTFILE.DAT", "w");
while ((read = getline(&line, &len, fp)) != -1) {
//printf("Retrieved line of length %zu : ", read);
int x = atoi(line);
if(x>80)
{
printf("%d ",x);
fprintf(fptr,"%d ", x);
}
}
return 0;
}
output file
100
82
88
90
100
85
93
96
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.