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
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 |
Step by step
Solved in 2 steps