GNG1106 Lab # 8 C Structures and Files Slides by: Mohamad Eid Contributors: Diana Inkpen, Alan Williams, Daniel Amyot
Grading Case Study 2 Write a grading program for a class with the following grading policies. a. There are five quizzes, each graded on the basis of 10 points. b. There are four assignments, each graded on the basis of 100 points. c. There is one midterm exam and one final exam, each graded on the basis of 100 points. d. The final exam counts for 45% of the grade, the midterm counts for 25%, and the five quizzes together count for a total of 10%, and the assignments count for 20%. The program will read in the students’ scores and output the student’s numeric and alphabetic scores for the entire course. The program will display the average of the quizzes, assignments, and the total score before it terminates. For alphebetical grading, use the following system: A -> B -> C -> D -> F-> less than 60
Grading Case Study – Cont’d 3 Define a structure to define the following information: - Student Name - Assignments (array of 4 real numbers) - Quizzes (array of 5 real numbers) - Midterm - Final - Score (final grade of the student) - GRADE (letter grade for the student) Define an array of structures for a class of 200 students. You might test your code with 2-5 students class. Some functions you might use: - InputGrades (prompt the user for all students grades) - ComputeScores (compute the final grade for all the students) - ComputeLetterGrades (determine the letter grade of the students) - printGradesReport (print table of student name, score, and letter grade) - Use other functions as needed!
File Pointer 4 In C, you access files through a variable called a "file pointer". A file pointer is a variable of type FILE. Here's how you declare a file pointer: FILE *fp; Here's how you make it point to a file called "results.dat" that you intend to write to: fp = fopen("results.dat", "w");
Opening a file 5 fp = fopen("results.dat", "w"); wwrite: used to create a new file or to erase an existing file and to write in it. rread: used to read an existing file. aappend: used to write data at the end of a file. w+ write+: used to create a new file or to erase the content of an existing file, and then to write to and to read from the file. r+read+: used to read from and to write to the file. a+append+: used to read from a file and to write at the end of the file.
Remark 6 For various reasons, it's possible that the call to fopen() can fail. It's always wise to make sure the file was opened correctly and if it doesn't, to quit the program. #include int main(void) { FILE *fp; fp = fopen("results.dat", "w"); if (fp == NULL) { printf("I couldn't open results.dat for writing.\n"); exit(0); } return 0; }
Let's take a look at how you write to the file. We'll write a list of numbers and their squares into results.dat fprintf(fp, "%d, %d\n", i, i*i); Writing to a file
Reading a file 8 Here's how you would read the file results.dat fscanf(fp, "%d,%d\n", &i, &isquared)
Closing a file After you're finished writing to the file, the last step is to close the file. You do that with the fclose() function. fclose(fp);
Exercise 1 Write a program that creates a file named “Numbers.txt” if it does not exist. Write 10 integers created randomly into the file using I/O functions. Integers are stored on separate lines. Check if the file stores the numbers.
Exercise 2 Write a program that will count the number of letters, words, and lines, in a file. Words are separated by spaces, tabs, or carriage return characters. The file name should be entered by the user. To test this program, you need to create a file with some text
Exercise 3 Write a program that checks if a file exists or not. If the file does not exist, then the program prompts the user whether he/she wants to create it or not. If yes, the program creates the file and prints a confirmation message to the user.
Exercise 4 Write a program that presents the user with a menu: 1. Add a student record 2. Print a student record 3. Exit Use two functions: A) addRecord() that creates a structure (student) and prompts the user for the student information and store the data in the structure, then creates a file named after the student number, and writes the information into the file. Note that files are created per student. B) printRecord() that prompts the user to enter the student number, opens the file for the student, and displays the student information on the screen. The function should alert the user if the student record does not exist.