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! #include #include //MAIN METHOD int main() { //Declare file pointers FILE *fin,*fpass,*ffail,*finvalid; //Declare variablef to store filename char str[30]; //Declare variables: option and ch int option,ch; //Read file name printf("Enter File Name: "); scanf("%s",str); //Open input file in read mode. fin = fopen(str,"r"); //Open Output file in write mode. fpass= fopen("passed.txt","w"); ffail = fopen("failed.txt","w"); finvalid = fopen("invalid.txt","w"); //Display error message if input file is not opened. if(fin == NULL) { printf("Error in opening %s!",str); exit(1); } //Read first integer from input file fscanf(fin,"%d",&ch); //Use While loop, to read all integers from file and place them in respective output files. while (!feof(fin)) { //if integer is with in 70-100 then put them in passed.txt file. if(ch<=100 && ch>=70) { fprintf(fpass,"%d\n",ch); } //If integer is with in 0-69 then put them in failed.txt file. else if(ch<70 && ch>=0) { fprintf(ffail,"%d\n",ch); } //otherwise put the integer in invalid.txt file else{ fprintf(finvalid,"%d\n",ch); } //read next integer from file fscanf(fin,"%d",&ch); } printf("\nAll grades are evaluated and stored in their respective files."); //Close all file pointers fclose(fpass); fclose(ffail); fclose(finvalid); //Display File menu printf("\nDISPLAY FILE MENU"); printf("\n[1] Passing\n[2] Failing\n[3] Invalid\nOption:\nPassing Grades: "); //Read option from user scanf("%d",&option); //Use switch, to display respective file content based on user choice switch(option) { case 1: //If case-1, Display passed.txt file information. //Open file in read mode fpass= fopen("passed.txt","r"); //Use while loop, to display all integers in passed.txt file. fscanf(fpass,"%d",&ch); while (!feof(fpass)) { printf("%d, ",ch); fscanf(fpass,"%d",&ch); } break; case 2: //If case-2, Display failed.txt file information. //Open file in read mode ffail = fopen("failed.txt","r"); //Use while loop, to display all integers in failed.txt file. fscanf(ffail,"%d",&ch); while (!feof(ffail)) { printf("%d, ",ch); fscanf(ffail,"%d",&ch); } break; case 3: //If case-3, Display invalid.txt file information. //Open file in read mode finvalid = fopen("invalid.txt","r"); //Use while loop, to display all integers in invalid.txt file. fscanf(finvalid,"%d",&ch); while (!feof(finvalid)) { printf("%d, ",ch); fscanf(finvalid,"%d",&ch); } break; default: printf("Invalid Option"); } }
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!
#include<stdio.h>
#include<stdlib.h>
//MAIN METHOD
int main()
{
//Declare file pointers
FILE *fin,*fpass,*ffail,*finvalid;
//Declare variablef to store filename
char str[30];
//Declare variables: option and ch
int option,ch;
//Read file name
printf("Enter File Name: ");
scanf("%s",str);
//Open input file in read mode.
fin = fopen(str,"r");
//Open Output file in write mode.
fpass= fopen("passed.txt","w");
ffail = fopen("failed.txt","w");
finvalid = fopen("invalid.txt","w");
//Display error message if input file is not opened.
if(fin == NULL)
{
printf("Error in opening %s!",str);
exit(1);
}
//Read first integer from input file
fscanf(fin,"%d",&ch);
//Use While loop, to read all integers from file and place them in respective output files.
while (!feof(fin))
{
//if integer is with in 70-100 then put them in passed.txt file.
if(ch<=100 && ch>=70)
{
fprintf(fpass,"%d\n",ch);
}
//If integer is with in 0-69 then put them in failed.txt file.
else if(ch<70 && ch>=0)
{
fprintf(ffail,"%d\n",ch);
}
//otherwise put the integer in invalid.txt file
else{
fprintf(finvalid,"%d\n",ch);
}
//read next integer from file
fscanf(fin,"%d",&ch);
}
printf("\nAll grades are evaluated and stored in their respective files.");
//Close all file pointers
fclose(fpass);
fclose(ffail);
fclose(finvalid);
//Display File menu
printf("\nDISPLAY FILE MENU");
printf("\n[1] Passing\n[2] Failing\n[3] Invalid\nOption:\nPassing Grades: ");
//Read option from user
scanf("%d",&option);
//Use switch, to display respective file content based on user choice
switch(option)
{
case 1:
//If case-1, Display passed.txt file information.
//Open file in read mode
fpass= fopen("passed.txt","r");
//Use while loop, to display all integers in passed.txt file.
fscanf(fpass,"%d",&ch);
while (!feof(fpass))
{
printf("%d, ",ch);
fscanf(fpass,"%d",&ch);
}
break;
case 2:
//If case-2, Display failed.txt file information.
//Open file in read mode
ffail = fopen("failed.txt","r");
//Use while loop, to display all integers in failed.txt file.
fscanf(ffail,"%d",&ch);
while (!feof(ffail))
{
printf("%d, ",ch);
fscanf(ffail,"%d",&ch);
}
break;
case 3:
//If case-3, Display invalid.txt file information.
//Open file in read mode
finvalid = fopen("invalid.txt","r");
//Use while loop, to display all integers in invalid.txt file.
fscanf(finvalid,"%d",&ch);
while (!feof(finvalid))
{
printf("%d, ",ch);
fscanf(finvalid,"%d",&ch);
}
break;
default:
printf("Invalid Option");
}
}
Step by step
Solved in 2 steps with 2 images