GNG1106 Lab # 8 C Structures and Files Slides by: Mohamad Eid Contributors: Diana Inkpen, Alan Williams, Daniel Amyot.

Slides:



Advertisements
Similar presentations
DATABASE BASICS: INSERTING AND FORMATTING DATA EXCEL 07 SESSION II.
Advertisements

FILES Files types and operations. Files Files are used to store data Data can be Text (ASCII only: 0  127) Binary (Full range: 0  256) Each file resides.
1 ICS103 Programming in C Lecture 8: Data Files. 2 Outline Why data files? Declaring FILE pointer variables Opening data files for input/output Scanning.
What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
1 Lab Session-III CSIT-120 Spring 2001 Revising Previous session Data input and output While loop Exercise Limits and Bounds GOTO SLIDE 13 Lab session.
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
Mastering Char to ASCII AND DOING MORE RELATED STRING MANIPULATION Why VB.Net ?  The Language resembles Pseudocode - good for teaching and learning fundamentals.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
ITI 1120 Lab #5 Contributors: S. Boyd, R. Plesa, A. Felty, D. Inkpen, A. Williams, D. Amyot.
ITI 1120 Lab #11 Contributors: Diana Inkpen, Daniel Amyot, Sylvia Boyd, Amy Felty, Romelia Plesa, Alan Williams.
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
Programming Fundamentals I Java Programming Spring 2009 Instructor: Xuan Tung Hoang TA: Tran Minh Trung Lab 03.
1 More on Readln:numerical values Note: ENTER key counts sends a carriage return and a line feed to the computer definition: “white space”: space, tab,
Data Types and Conversions, Input from the Keyboard If you can't write it down in English, you can't code it. -- Peter Halpern If you lie to the computer,
Exercise 1 #include int main() { printf(“Hello C Programming!\n”); return 0; } 1.Run your Visual Studio 2008 or Create a new “project” and add.
1 CSC103: Introduction to Computer and Programming Lecture No 27.
Files A collection of related data treated as a unit. Two types Text
FILES IN C. File Operations  Creation of a new file  Opening an existing file  Reading from a file  Writing to a file  Moving to a specific location.
Chapter 6 Persistence-Saving and Retrieving Data
CIS 115 Slingshot Academy / cis115.com
Lesson #5 Repetition and Loops.
Some Assignments  Write a program which prints the following information about at least 5 persons: NAME MAIL-ID EMPLOYEE-CODE PHONE Eg. Umesh
ECE Application Programming
Data Types and Conversions, Input from the Keyboard
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
Lesson #5 Repetition and Loops.
Different Types of Testing
CS1010 Programming Methodology
CSE1320 Files in C Dr. Sajib Datta
CSE1320 Files in C Dr. Sajib Datta
Engineering Innovation Center
CIS 115 Possible Is Everything/snaptutorial.com
CIS115 Education for Service-- snaptutorial.com
CIS 115 Teaching Effectively-- snaptutorial.com
For Monday Read WebCT quiz 18.
Control Statement Examples
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
CSE1320 Files in C Dr. Sajib Datta
File I/O We are used to reading from and writing to the terminal:
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
One-Dimensional Array Introduction Lesson xx
Lesson #5 Repetition and Loops.
CSE1320 Strings Dr. Sajib Datta
IPC144 Week 10 – Lesson 2 Working with Files
For Wednesday No new reading No quiz.
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
FILE HANDLING IN C.
The while Looping Structure
Files.
Spreadsheets, Modelling & Databases
Flowcharts and Pseudo Code
Functions continued.
CPS120: Introduction to Computer Science
Reading and Writing Text Files
Suggested self-checks: Section 7.11 #1-11
Lesson #5 Repetition and Loops.
EECE.2160 ECE Application Programming
More Loops Topics Relational Operators Logical Operators for Loops.
National Chiao Tung University
The while Looping Structure
ICS103 Programming in C Lecture 12: Arrays I
The while Looping Structure
EECE.2160 ECE Application Programming
ICS103: Programming in C 6: Pointers and Modular Programming
Professor Jodi Neely-Ritz University of Florida
Switch Case Structures
File I/O We are used to reading from and writing to the terminal:
Files Chapter 8.
Presentation transcript:

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.