#include <stdlib.h> #include <stdio.h> #include <math.h> int main(void) { int ID
ID: 3626186 • Letter: #
Question
#include <stdlib.h>#include <stdio.h>
#include <math.h>
int main(void)
{
int IDNum = 0;
float setofGrades = 0.0;
float GPA = 0.0;
char Studentid[8];
FILE *infile;
FILE *outfile;
infile = fopen(SpringGrades.dat, "r");
if(!infile)
{
printf("Could not open the file that you entered ");
getchar();
getchar();
return 1;
}
outfile = fopen(WinterGradesPlus.dat, "w");
fprintf(WinterGradesPlus.dat, "Fred Smith ");
fprintf(WinterGradesPlus.dat, "1234-12-567 ");
fprintf(WinterGradesPlus.dat, "3.9 3.1 4.0 1.0 4.0 ");
fprintf(WinterGradesPlus.dat, "GPA: 3.2 ");
fclose(WinterGradesPlus.dat);
fclose(SpringGrades.dat);
getchar();
getchar();
return EXIT_SUCCESS;
}
What i need to do is i have to write a C porogram which will read the data from the original file which is SpringGrades.dat, increase each grade by one full point (max 4.0) and write the full name, Student id, and new grades in the same format to a new file called WinterGradesPlus.dat.
Explanation / Answer
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int main(void)
{
int IDNum = 0;
float grade = 0.0;
float GPA = 0.0;
char Names[20];
char Studentid[8];
FILE *infile;
FILE *outfile;
infile = fopen("SpringGrades.dat", "r");
outfile = fopen("WinterGradesPlus.dat", "w");
if(!infile)
{
printf("Could not open the file that you entered ");
return 1;
}
else
{
while(infile)
{
fgets(Names, 20, infile); /* read the name from the file */
fgets(Studentid, 8, infile); /* read the name from the file */
int i=0;
fprintf(outfile,"%s ",Names);
fprintf(outfile,"%s ",Studentid);
while(i<4)
{
fscanf(infile,"%d", &grade);
grade+=1;
i++;
fprintf(outfile,"%d ",grade);
}
}
}
fclose(infile);
fclose(outfile);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.