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
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:
Filename to read: grades.txt
Filename to write: passed.txt, failed.txt, invalid.txt
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;
}
Step by step
Solved in 4 steps with 7 images