Need help! please fix my code  #include #include #include int main() { char name[50]; printf("Enter File Name:\n"); scanf("%s",name); FILE *fp = fopen(name, "r"); if (fp == NULL) { perror("The file can't be open."); 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; } GIVEN OUTPUT 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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Need help! please fix my code 

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


int main()
{
char name[50];
printf("Enter File Name:\n");
scanf("%s",name);
FILE *fp = fopen(name, "r");
if (fp == NULL)
{
perror("The file can't be open.");
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;
}

GIVEN OUTPUT

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

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education