1. include include LAST RUN on 328/2021, 349 40 PM include int main(){ int temp, opt; int a[10]; FILE fp2, fp3, fp4; // open file FILE myFile = fopen("grades.txt", "r"); // if opening file fails, print eror message and exit 1 if (myFile==NULL){ perror ("Error: Failed to open file."); exit(1); 3 Check 1 failed 4. Output: 5 6 7 [1] Passing [2] Failing 9 [3] Invalid 10 11. Option: 12 Invalid Option 13 Expected: 14 Enter File Name: //read values from file until EOF is returned by fscanf for (int i=0;i=70){ 25 Check 2 failed 26 Output: 27 28 [1] Passing 29 . 30- [2] Failing 31 putw(a[1], fp2); [3] Invalid 32 Option: else if(a[i]>=0||a<7®){ putw(a[i],fp3); 33 - 34 Invalid Option 35 Expected: else if(a[i]<0||a>100){ putw(a[i],fp4); 36 . Enter File Name: 37 All grades are evaluated and stored in their respective files. 38 39 DISPLAY FILE MENU fclose(fp2); fclose(fp3); fclose(fp4); printf("\n[1] Passing\n[2] Failing\n[3] Invalid\n0ption: "); 40 [1] Passing 41 [2] Failing 42 [3] Invalid 43 44 scanf("%d",&opt): Option: Failing Grades: 5e, e, 69, 1, e (1:18) 35 Check 3 failed else if(a[i]<0[|a>100){ putw(a[i], fp4); 36 - Output: 37 38 39 [1] Passing fclose(fp2); fclose (fp3); fclose (fp4); printf("\n[1] Passing\n[2] Failing\n[3] Invalid\n0ption: "); scanf("%d", &opt); int x; if(opt==1){ 40 [2] Failing 41 [3] Invalid 42 43 Option: 44 Invalid Option 45 Expected: 46. Enter File Name: printf("\nPassing Grades: "); fp2=fopen("passed. txt","r"); while(x!=-1){ 47 48 All grades are evaluated and stored in their respective files. 49. DISPLAY FILE MENU x=fgetc(fp2); if(x<=®) printf(""); else printf("%d ",x); 50 [1] Passing 51 52 [2] Failing 53 [3] Invalid 54 Option: 55 Invalid Grades: 101, -1, 56 printf("\n"); 57 Check 4 failed else if(opt==2){ 58. Output: printf("\nFailing Grades: "); fp3=fopen("failed.txt","r"); 59 60 [1] Passing 61 - while(x!=-1){ 62 [2] Failing x=fgetc(fp3); if(x<=®) printf(""); else{ printf("%d ",x); 63 [3] Invalid 64 Option: 65 . Invalid Option 66 67 Expected: 68 Enter File Name: 69 printf("\n"); All grades are evaluated and stored in their respective files. 70 71. else if(opt==3){ DISPLAY FILE MENU 72 printf("\nInvalid Grades: "); [1] Passing 73 fp4=fopen("invalid.txt","r"); while(x!=-1){ [2] Failing 74 . [3] Invalid 75 x=fgetc(fp3); printf("%d ",x); 76 Option: 77 Invalid Option 78 printf("\n"); Check 5 failed es (1:18) Output: DISPLAY FILE MENU printf("\n[1] Passing\n[2] Failing\n[3] Invalid\n0ption: "); scanf ("id", &opt); int x; if(opt==1){ 43 [1] Passing 45 [2] Failing [3] Invalid printf("\nPassing Grades: "); fp2=fopen("passed. txt","r"); while(x:=-1){ 47 Option: 48 49. Invalid Grades: 101, -1, x=fgetc(fp2); if(x<=®) printf(""); else 50 Check 4 failed 51 Output: 52 53 54 printf("id ",x); [1] Passing 55 [2] Failing 56 printf("\n"); [3] Invalid 57 else if(opt==2){ Option: 58- printf("\nFailing Grades: "): fp3=fopen("failed.txt","r"); while(x:=-1){ 59 Invalid Option 60 Expected: 61- Enter File Name: x=fgetc(fp3); if(x<=0) printf(""); else{ printf("id ",x); 62 63 All grades are evaluated and stored in their respective files. 64 DISPLAY FILE MENU 65. [1] Passing 66 [2] Failing 67 68 [3] Invalid 69 printf("\n"); Option: 70 Invalid Option else if(opt==3){ 71- printf("\nInvalid Grades: "); fp4=fopen ("invalid.txt","r"); while(x:=-1){ 72 Check 5 failed 73 Output: 74 - x=fgetc(fp3) ; printf("%d ",x); 75 [1] Passing 76 77 [2] Failing 78 printf("\n"); [3] Invalid 79 Option: else{ printf("\nInvalid Option\n"); 80 - 81 Invalid Option 82 Еxpected: 83 return e; Enter File Name: 84 The file can't be open. File does not exists. AK Show diff * (1:18)
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? please look at the expected results I'll rate!
My code
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
int temp,opt;
int a[10];
FILE *fp2,*fp3,*fp4;
// open file
FILE *myFile = fopen("grades.txt", "r");
// if opening file fails, print error message and exit 1
if (myFile==NULL){
perror("Error: Failed to open file.");
exit(1);
}
//read values from file until EOF is returned by fscanf
for(int i=0;i<10;++i){
// assign the read value to variable (tek), and enter it in array (radica)
if(fscanf(myFile,"%d",&temp) == 1) {
a[i]=temp;
}
else{
break;
}
}
fclose(myFile);
fp2=fopen("passed.txt","w");
fp3=fopen("failed.txt","w");
fp4=fopen("invalid.txt","w");
for (int i=0;i<10;++i){
if(a[i]>=70){
putw(a[i],fp2);
}
else if(a[i]>=0||a<70){
putw(a[i],fp3);
}
else if(a[i]<0||a>100){
putw(a[i],fp4);
}
}
fclose(fp2);
fclose(fp3);
fclose(fp4);
printf("\n[1] Passing\n[2] Failing\n[3] Invalid\nOption: ");
scanf("%d",&opt);
int x;
if(opt==1){
printf("\nPassing Grades: ");
fp2=fopen("passed.txt","r");
while(x!=-1){
x=fgetc(fp2);
if(x<=0)
printf("");
else
printf("%d ",x);
}
printf("\n");
}
else if(opt==2){
printf("\nFailing Grades: ");
fp3=fopen("failed.txt","r");
while(x!=-1){
x=fgetc(fp3);
if(x<=0)
printf("");
else{
printf("%d ",x);
}
}
printf("\n");
}
else if(opt==3){
printf("\nInvalid Grades: ");
fp4=fopen("invalid.txt","r");
while(x!=-1){
x=fgetc(fp3);
printf("%d ",x);
}
printf("\n");
}
else{
printf("\nInvalid Option\n");
}
return 0;
}
Step by step
Solved in 4 steps with 8 images