/* file3.c Author: James Tam Version: January 12, 2010 Assumptions: 1) The input file is in the following format 2) The maximum length of the name of the input file (and the path) does not exceed 256 characters. This program will read numeric grade points from a file and based on these values calculate a term letter grade. The user will be prompted to enter the name of the file at run time. The letter grades read from the input file will be then written to an output file called "letters.txt". */ #include int const MAX = 256; int main () { FILE *input_fp; FILE *output_fp; int midterm_exam; int final_exam; int assignments; float term_gpa; char letter; char input_file_name [MAX]; printf ("Enter the name of the file that contains the numeric grade points: "); scanf ("%s", input_file_name); input_fp = fopen (input_file_name, "r"); output_fp = fopen ("letters.txt", "w"); if (input_fp == NULL) printf ("File '%s' cannot be opened.", input_file_name); else if (output_fp == NULL) printf ("Unable to open output file letters.txt for writing."); else { while (fscanf (input_fp, "%d", &midterm_exam) >= 0) { fscanf (input_fp, "%d", &final_exam); fscanf (input_fp, "%d", &assignments); term_gpa = (midterm_exam * 0.3) + (final_exam * 0.4) + (assignments * 0.3); /* JT's note: these cutoffs are for use in this program only and do not necessarily correspond to the ones used in the course. */ if ((term_gpa <= 4) && (term_gpa > 3.5)) letter = 'A'; else if ((term_gpa <= 3.5) && (term_gpa > 2.5)) letter = 'B'; else if ((term_gpa <= 2.5) && (term_gpa > 1.5)) letter = 'C'; else if ((term_gpa <= 1.5) && (term_gpa > 0.5)) letter = 'D'; else if ((term_gpa <= 0.5) && (term_gpa >= 0)) letter = 'F'; else printf ("Error in gpa"); printf ("%0.2f %c\n", term_gpa, letter); fprintf (output_fp, "%c\n", letter); } /* While: read from file */ fclose (input_fp); fclose (output_fp); } /* Else: if program can read from the file. */ } /* End of the program. */