Write a C program that will get 10 grades of the student from a file named grades.txt. The program will check each grade if its passing, failing or invalid. Passing grade is 70-100. Failing grade is 0-69. Other value will be considered

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Write a C program that will get 10 grades of the student from a file named grades.txt. The program will check each grade if its passing, failing or invalid. Passing grade is 70-100. Failing grade is 0-69. Other value will be considered invalid. Store all the passing grade to an output file named passed.txt. Store all the failing grades to an output file named failed.txt. And store all the invalid grades to another output file named invalid.txt. The program must comply with the given sample output below:

NOTE:
Filename to read: grades.txt
Filename to write: passed.txt, failed.txt, invalid.txt
NOTE:
Your source code must display any of the given sample output below.
It means your source code should be flexible enough to meet any of the given sample output.
Your source code output must be identical to any of the given sample output.
It means you have to strictly follow what are the displayed text, labels, casing of characters in the sample output.
Strictly follow the naming of file.
Sample OUTPUT1:
Enter File Name: grades.txt
All grades are evaluated and stored in their respective files.
DISPLAY FILE MENU
[1] Passing
[2] Failing
[3] Invalid
Option: 1
Passing Grades: 100, 90, 70, 99,
Sample OUTPUT2:
Enter File Name: grades.txt
All grades are evaluated and stored in their respective files.
DISPLAY FILE MENU
[1] Passing
[2] Failing
[3] Invalid
Option: 2
Failing Grades: 50, 0, 69, 1,
Sample OUTPUT3:
Enter File Name: grades.txt
All grades are evaluated and stored in their respective files.
DISPLAY FILE MENU
[1] Passing
[2] Failing
[3] Invalid
Option: 3
Invalid Grades: 101, -1,
Sample OUTPUT4:
Enter File Name: grades.txt
All grades are evaluated and stored in their respective files.
DISPLAY FILE MENU
[1] Passing
[2] Failing
[3] Invalid
Option: 4
Invalid Option
Sample OUTPUT5:
Enter File Name: grade.txt
The file can’t be open. File does not exists.

Please help how to fix my Code! 

My Code I'll rate! !!!!  !!! 

#include <stdio.h>

#include <stdlib.h>
#include <string.h>

// Driver code
int main()
{
char name[50];
printf("\nEnter File Name:\n ");
scanf("%s",name);
FILE *fp = fopen(name, "r");
if (fp == NULL)
{
perror("The file can’t be open. File does not exist.");
exit(1);
}

char chunk[128];
FILE *fptr1;
fptr1 = fopen("passed.txt", "w");
FILE *fptr2;
fptr2 = fopen("failed.txt", "w");
FILE *fptr3;
fptr3 = fopen("invalid.txt", "w");

while (fgets(chunk, sizeof(chunk), fp) != NULL)
{
int x = atoi(chunk);
if(x>=70 && x<=100)
{
fprintf(fptr1, "%d, ", x);
}
else if (x <= 69 && x >= 0)
{
fprintf(fptr2, "%d, ", x);
}
else
{
fprintf(fptr3, "%d, ", x);
}
}
printf("All grades are evaluated and stored in their respective files.");
fclose(fp);
fclose(fptr1);
fclose(fptr2);
fclose(fptr3);
printf("\nDISPLAY FILE MENU\n");
printf("[1] Passing\n");
printf("[2] Failing\n");
printf("[3] Invalid\n");
printf("Option: ");
int choice;
scanf("%d", &choice);
switch(choice)
{
case 1:
fptr1 = fopen("passed.txt", "r");
while (fgets(chunk, sizeof(chunk), fp) != NULL)
{
printf("\n\t\tPassing Grades: %s\n", chunk);
}
fclose(fptr1);
break;
case 2:
fptr1 = fopen("failed.txt", "r");
while (fgets(chunk, sizeof(chunk), fp) != NULL)
{
printf("\n\t\tFailing Grades: %s\n", chunk);
}
fclose(fptr1);
break;
case 3:
fptr1 = fopen("invalid.txt", "r");
while (fgets(chunk, sizeof(chunk), fp) != NULL)
{
printf("\n\t\tInvalid Grades: %s\n", chunk);
}
fclose(fptr1);
break;
default:
printf("\t\t\tInvalid Option\n");
}

return 0;
}

#include <stdio.h>
2
#include <stdlib.h>
LAST RUN on 3/28/2021, 5:25:06 PM
3
#include <string.h>
Check 1 failed
4
Output:
// Driver code
int main()
6
{
char name [50];
printf("\nEnter File Name:\n ");
scanf("%s", name);
FILE *fp = fopen (name, "r");
if (fp == NULL)
Enter File Name:
8
All grades are evaluated and stored in their respective files.
9
DISPLAY FILE MENU
10
11
[1] Passing
12
[2] Failing
13 -
{
[3] Invalid
perror ("The file can't be open. File does not exist.");
exit(1);
}
14
Option:
15
16
Expected:
17
Enter File Name:
char chunk[128];
FILE *fptr1;
fptrl = fopen ("passed.txt", "w");
FILE *fptr2;
fptr2 = fopen ("failed.txt", "w");
FILE *fptr3;
fptr3 = fopen ("invalid.txt", "w");
18
All grades are evaluated and stored in their respective files.
19
DISPLAY FILE MENU
20
21
[1] Passing
22
[2] Failing
23
[3] Invalid
24
25
Option:
while (fgets(chunk, sizeof(chunk), fp) != NULL)
{
int x = atoi(chunk);
26
Passing Grades: 100, 90, 70, 99,
27 -
Check 2 failed
28
Output:
if(x>=70 && x<=100)
{
29
30 -
fprintf(fptr1, "%d, ", x);
}
else if (x <= 69 && x >= 0)
{
31
Enter File Name:
32
All grades are evaluated and stored in their respective files.
33
34 -
DISPLAY FILE MENU
fprintf(fptr2, "%d, ", x);
}
35
[1] Passing
36
[2] Failing
37
else
[3] Invalid
38 -
{
Option:
fprintf(fptr3, "%d, ", x);
}
}
39
40
Expected:
41
Enter File Name:
printf("All grades are evaluated and stored in their respective files.");
fclose (fp);
fclose (fptrl);
42
All grades are evaluated and stored in their respective files.
43
44
DISPLAY FILE MENU
[1] Passing
49% (42:12)
DISPLAY FILE MENU
fclose (fptr1);
fclose (fptr2);
fclose (fptr3);
printf("\NDISPLAY FILE MENU\n");
printf("[1] Passing\n");
printf("[2] Failing\n");
printf("[3] Invalid\n");
printf("Option: ");
int choice;
scanf("%d", &choice);
switch (choice)
{
44
45
[1] Passing
46
[2] Failing
47
[3] Invalid
48
Option:
49
50
Failing Grades: 50, 0, 69, 1,
51
Check 3 failed
52
Output:
53
54
55 -
Enter File Name:
56
case 1:
All grades are evaluated and stored in their respective files.
fptrl = fopen ("passed.txt", "r");
while (fgets (chunk, sizeof(chunk), fp) != NULL)
57
DISPLAY FILE MENU
58
59 -
{
[1] Passing
printf("\n\t\tPassing Grades: %s\n", chunk);
}
fclose (fptr1);
break;
60
[2] Failing
61
[3] Invalid
62
Option:
63
64
case 2:
Expected:
fptr1 = fopen ("failed.txt", "r");
while (fgets (chunk, sizeof(chunk), fp) != NULL)
65
Enter File Name:
66
All grades are evaluated and stored in their respective files.
67 -
{
DISPLAY FILE MENU
printf("\n\t\tFailing Grades: %s\n", chunk);
}
fclose (fptr1);
break;
68
69
[1] Passing
70
[2] Failing
71
[3] Invalid
72
case 3:
fptrl = fopen ("invalid.txt", "r");
while (fgets (chunk, sizeof(chunk), fp) != NULL)
73
Option:
74
Invalid Grades: 101, -1,
75 -
{
check 4 passed
printf("\n\t\tInvalid Grades: %s\n", chunk);
}
fclose (fptr1);
break;
default:
76
check 5 failed
77
78
Output:
79
80
Enter File Name:
81
printf("\t\t\tInvalid Option\n");
82
}
The file can't be open. File does not exist.: No such file or directory
83
Expected:
84
return 0;
Enter File Name:
85
}
The file can't be open. File does not exists.
YK
Show diff
0% (1:18)
Transcribed Image Text:#include <stdio.h> 2 #include <stdlib.h> LAST RUN on 3/28/2021, 5:25:06 PM 3 #include <string.h> Check 1 failed 4 Output: // Driver code int main() 6 { char name [50]; printf("\nEnter File Name:\n "); scanf("%s", name); FILE *fp = fopen (name, "r"); if (fp == NULL) Enter File Name: 8 All grades are evaluated and stored in their respective files. 9 DISPLAY FILE MENU 10 11 [1] Passing 12 [2] Failing 13 - { [3] Invalid perror ("The file can't be open. File does not exist."); exit(1); } 14 Option: 15 16 Expected: 17 Enter File Name: char chunk[128]; FILE *fptr1; fptrl = fopen ("passed.txt", "w"); FILE *fptr2; fptr2 = fopen ("failed.txt", "w"); FILE *fptr3; fptr3 = fopen ("invalid.txt", "w"); 18 All grades are evaluated and stored in their respective files. 19 DISPLAY FILE MENU 20 21 [1] Passing 22 [2] Failing 23 [3] Invalid 24 25 Option: while (fgets(chunk, sizeof(chunk), fp) != NULL) { int x = atoi(chunk); 26 Passing Grades: 100, 90, 70, 99, 27 - Check 2 failed 28 Output: if(x>=70 && x<=100) { 29 30 - fprintf(fptr1, "%d, ", x); } else if (x <= 69 && x >= 0) { 31 Enter File Name: 32 All grades are evaluated and stored in their respective files. 33 34 - DISPLAY FILE MENU fprintf(fptr2, "%d, ", x); } 35 [1] Passing 36 [2] Failing 37 else [3] Invalid 38 - { Option: fprintf(fptr3, "%d, ", x); } } 39 40 Expected: 41 Enter File Name: printf("All grades are evaluated and stored in their respective files."); fclose (fp); fclose (fptrl); 42 All grades are evaluated and stored in their respective files. 43 44 DISPLAY FILE MENU [1] Passing 49% (42:12) DISPLAY FILE MENU fclose (fptr1); fclose (fptr2); fclose (fptr3); printf("\NDISPLAY FILE MENU\n"); printf("[1] Passing\n"); printf("[2] Failing\n"); printf("[3] Invalid\n"); printf("Option: "); int choice; scanf("%d", &choice); switch (choice) { 44 45 [1] Passing 46 [2] Failing 47 [3] Invalid 48 Option: 49 50 Failing Grades: 50, 0, 69, 1, 51 Check 3 failed 52 Output: 53 54 55 - Enter File Name: 56 case 1: All grades are evaluated and stored in their respective files. fptrl = fopen ("passed.txt", "r"); while (fgets (chunk, sizeof(chunk), fp) != NULL) 57 DISPLAY FILE MENU 58 59 - { [1] Passing printf("\n\t\tPassing Grades: %s\n", chunk); } fclose (fptr1); break; 60 [2] Failing 61 [3] Invalid 62 Option: 63 64 case 2: Expected: fptr1 = fopen ("failed.txt", "r"); while (fgets (chunk, sizeof(chunk), fp) != NULL) 65 Enter File Name: 66 All grades are evaluated and stored in their respective files. 67 - { DISPLAY FILE MENU printf("\n\t\tFailing Grades: %s\n", chunk); } fclose (fptr1); break; 68 69 [1] Passing 70 [2] Failing 71 [3] Invalid 72 case 3: fptrl = fopen ("invalid.txt", "r"); while (fgets (chunk, sizeof(chunk), fp) != NULL) 73 Option: 74 Invalid Grades: 101, -1, 75 - { check 4 passed printf("\n\t\tInvalid Grades: %s\n", chunk); } fclose (fptr1); break; default: 76 check 5 failed 77 78 Output: 79 80 Enter File Name: 81 printf("\t\t\tInvalid Option\n"); 82 } The file can't be open. File does not exist.: No such file or directory 83 Expected: 84 return 0; Enter File Name: 85 } The file can't be open. File does not exists. YK Show diff 0% (1:18)
Expert Solution
steps

Step by step

Solved in 4 steps with 7 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY