File processing with functions H&K Chapter 3 Instructor – Gokcen Cilingir Cpt S 121 (June 27, 2011) Washington State University.

Slides:



Advertisements
Similar presentations
File Management in C. What is a File? A file is a collection of related data that a computers treats as a single unit. Computers store files to secondary.
Advertisements

BITS Pilani, Pilani Campus TA C252 Computer Programming - II Vikas Singh File Handling.
File Management in C. A file is a collection of related data that a computers treats as a single unit. File is a collection of data stored permanently.
Files in C Rohit Khokher. Files in C Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two.
Chapter 11: Data Files & File Processing In this chapter, you will learn about Files and streams Creating a sequential access file Reading data from a.
Functions H&K Chapter 3 Instructor – Gokcen Cilingir Cpt S 121 (June 23, 2011) Washington State University.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Lec11: File Processing 廖雪花 TEL: 年 5 月.
17 File Processing. OBJECTIVES In this chapter you will learn:  To create, read, write and update files.  Sequential file processing.  Random-access.
Guide To UNIX Using Linux Third Edition
V-1 University of Washington Computer Programming I File Input/Output © 2000 UW CSE.
Lecture 11 – Files Operation. Introduction Almost all of the program developed before this is interactive In interactive environment, input is via keyboard.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students C File Input and Output Checking input for errors.
C File Processing. Objectives To be able to create, write and update files. To become familiar with sequential access file processing. To become familiar.
1 Structure part 1 & File Processing. 2 Structures.
A First Book of ANSI C Fourth Edition Chapter 10 Data Files.
Lone Leth Thomsen Input / Output and Files. April 2006Basis-C-8/LL2 sprintf() and sscanf() The functions sprintf() and sscanf() are string versions of.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
File Handling Spring 2013Programming and Data Structure1.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 7P. 1Winter Quarter File I/O in C Lecture.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Programming Practice Introduction Tree Operations. Binary Search Tree. File Processing Create, read, write and update files. Sequential.
(2-2) Functions I H&K Chapter 3 Instructor - Andrew S. O’Fallon CptS 121 (Spetember 9, 2015) Washington State University.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
1. Introduction File Declaration and Initialization Creating and Opening File Closing File EOF Reading from and Writing into a File Extra : Random Access.
Introduction to Programming Using C Files. 2 Contents Files Working with files Sequential files Records.
Chapter 8 File-Oriented Input and Output. 8.1 INTRODUCTION a file can also be designed to store data. We can easily update files, A data file as input.
Chapter 8 : Binary Data Files1 Binary Data Files CHAPTER 8.
1 Chapter 7 – Object-Oriented Programming and File Handling spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information.
CSEB114: Principle of programming Chapter 11: Data Files & File Processing.
Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()
Chapter 11 File Processing. Objectives In this chapter, you will learn: –To be able to create, read, write and update files. –To become familiar with.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Introduce some standard library functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 11 – File Processing Outline 11.1Introduction.
Chapter 7 : File Processing1 File-Oriented Input & Output CHAPTER 7.
1 CHAPTER6 CHAPTER 6. Objectives: You’ll learn about;  Introduction  Files and streams  Creating a sequential access file  Reading data from a sequential.
Chapter 7 Files By C. Shing ITEC Dept Radford University.
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:
(3-1) Functions II H&K Chapter 3 Instructor - Andrew S. O’Fallon CptS 121 (September 9, 2015) Washington State University.
(3-2) File Processing with Functions Instructor - Andrew S. O’Fallon CptS 121 (September 11, 2015) Washington State University.
chap8 Chapter 12 Files (reference: Deitel ’ s chap 11)
C File Processing. Objectives To be able to create, write and update files. To become familiar with sequential access file processing. To become familiar.
Chapter 12 Files (reference: Deitel ’ s chap 11) chap8.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 29 Thanks for Lecture Slides:
Learners Support Publications Working with Files.
Streams and File Processing in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
 2007 Pearson Education, Inc. All rights reserved. 1 C File Processing.
Lecture 20: C File Processing. Why Using Files? Storage of data in variables and arrays is temporary Data lost when a program terminates. Files are used.
Connecting to Files In order to read or write to a file, we need to make a connection to it. There are several functions for doing this. fopen() – makes.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
UniMAP SemI-11/12EKT120: Computer Programming1 Files.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
UniMAP SemI-11/12EKT120: Computer Programming1 Files.
11 C File Processing.
TMF1414 Introduction to Programming
EKT120: Computer Programming
Introduction to Computer Programming Lecture 18 Binary Files
CS111 Computer Programming
What you need for the 1st phase of project
Chapter 11 – File Processing
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Text and Binary File Processing
Review & Lab assignments
Fundamental of Programming (C)
Files.
Chapter 12: File I/O.
Files Chapter 8.
Presentation transcript:

File processing with functions H&K Chapter 3 Instructor – Gokcen Cilingir Cpt S 121 (June 27, 2011) Washington State University

C. Hundhausen, A. O’Fallon2 Why Files? Need to store data and information outside of a program Most real applications need to create, update, and/or delete data and information Easy to process and manipulate

C. Hundhausen, A. O’Fallon3 Files and Streams in C (1) C views each file as a sequential stream of bits (1’s and 0’s) or bytes Each file ends with an end-of-file marker (EOF) Once a file is opened a stream is associated with it

C. Hundhausen, A. O’Fallon4 Files and Streams in C (2) When a program starts execution, three files and associated streams are automatically opened ◦ standard input (allows for us to get data from keyboard) ◦ standard output (allows for us to write to the screen) ◦ standard error Streams provide communication channels between files and programs

C. Hundhausen, A. O’Fallon5 How to Get Started with Files in C? Before files may be manipulated, they must first be opened ◦ Opening a file creates a communication channel between the file and the program Once a file is opened, several standard library functions are available to process file data and information Once all information and data associated with the file is no longer needed, it should be closed

C. Hundhausen, A. O’Fallon6 File Processing Algorithm Step 1: open the desired file ◦ Opening is based on filename and permissions (read, write, or append) ◦ Creates a new stream Step 2: process the file ◦ Read data from the file  Does not affect file ◦ Write data to the file  Completely overwrites existing file ◦ Add data to the end of the file  Retains previous information in file Step 3: close the file ◦ Destroys the stream

C. Hundhausen, A. O’Fallon7 File Functions in C Located in Open a file: ◦ fopen () – returns a file handle to opened file Read from a file: ◦ fscanf () Write to a file: ◦ fprintf () Close a file: ◦ fclose () – closes file based on file handle

C. Hundhausen, A. O’Fallon8 Problem Solving Example Revisited (1) Problem Statement: Write a program that computes your grade point average after completion of 3 courses. Inputs from a file: ◦ Grade point and number of credits for course 1 ◦ Grade point and number of credits for course 2 ◦ Grade point and number of credits for course 3 Outputs to a file: ◦ Grade point average (GPA) Relevant formula: GPA = ((grade_point1 * num_credits1) + (grade_point2 * num_credits2) + (grade_point3 * num_credits3) ) / total_num_credits

C. Hundhausen, A. O’Fallon9 Problem Solving Example (2) Initial algorithm ◦ Open the data files ◦ Get the grade points earned for each class from input file ◦ Get the credit hours for each class from input file ◦ Compute the average of the grade points ◦ Write the results to output file ◦ Close the files

C. Hundhausen, A. O’Fallon10 Problem Solving Example (3) Refined algorithm ◦ Open the data files  Open one file with read permissions (input file)  Open one file with write permissions (output file) ◦ Get the grade points earned for each class from input file ◦ Get the credit hours for each class from input file ◦ Compute the total number of credits total_num_credits = num_credits1 + num_credits2 + num_credits3; ◦ Compute the credits hours earned weighted_credits = (grade_point1 * num_credits1) + (grade_point2 * num_credits2) + (grade_point3 * num_credits3); ◦ Compute the average of the grade points gpa = weighted_credits / total_num_credits; ◦ Write the results to output file  Write total_num_credits  Write weighted_credits  Write gpa ◦ Close the files  Close the input file  Close the output file

C. Hundhausen, A. O’Fallon11 Problem Solving Example (4) The GPA example revisited (now with file processing) double get_grade_point (FILE *infile); int get_credits (FILE *infile); int compute_total_num_credits (int num_credits1, int num_credits2, int num_credits3); double compute_weighted_credits (double grade_point1, double grade_point2, double grade_point3, int num_credits1, int num_credits2, int num_credits3); double compute_gpa (double weighted_credits, int total_num_credits); void display_gpa (FILE * outfile, double weighted_credits, int total_num_credits, double gpa); int main (void) { int num_credits1 = 0, num_credits2 = 0, num_credits3 = 0; double grade_point1 = 0.0, grade_point2 = 0.0, grade_point3 = 0.0, weighted_credits = 0.0, total_num_credits = 0.0, gpa = 0.0; FILE * infile = NULL, *outfile = NULL; /* Variables that will allow for manipulation of our file streams */ /* Need to open an input file and output file */ infile = fopen (“input.txt”, “r”); /* Input file opened with read permisions “r” */ outfile = fopen (“output.txt”, “w”); /* Output file opened with write permissions “w” */ /* Get the grade points and credits from the input file */ /* The input file, “input.txt”, stores the grade point and number of credits for a class on separate lines */ grade_point1 = get_grade_point (infile); num_credits1 = get_credits (infile); grade_point2 = get_grade_point (infile); num_credits2 = get_credits (infile); grade_point3 = get_grade_point (infile); num_credits3 = get_credits (infile);

C. Hundhausen, A. O’Fallon12 Problem Solving Example (5) /* Sum up the credits for each course */ total_num_credits = compute_total_num_credits (num_credits1, num_credits2, num_credits3); /* Compute credit hours earned */ weighted_credits = compute_weighted_credits (grade_point1, grade_point2, grade_point3, num_credits1, num_credits2, num_credits3); /* Compute gpa */ gpa = compute_gpa (weighted_credits, total_num_credits); /* Write the results to output file, “output.txt” */ display_gpa (outfile, weighted_credits, total_num_credits, gpa); /* Don’t forget to close your files! */ fclose (infile); fclose (outfile); return 0; }

C. Hundhausen, A. O’Fallon13 Problem Solving Example (6) Definition of get_grade_point () /* Reads a grade point earned for a class from a file */ double get_grade_point (FILE *infile) { double grade_point = 0.0; fscanf (infile, "%lf", &grade_point); return grade_point; }

C. Hundhausen, A. O’Fallon14 Problem Solving Example (7) Definition of get_credits () /* Reads a the number of credits earned for a class from a file */ int get_credits (FILE *infile) { int num_credits = 0; fscanf (infile, "%d", &num_credits); return num_credits; }

C. Hundhausen, A. O’Fallon15 Problem Solving Example (8) Definition of compute_total_num_credits () /* Sums up the total number of credits earned for 3 courses */ Int compute_total_num_credits (int num_credits1, int num_credits2, int num_credits3) { int total_num_credits = 0; total_num_credits = num_credits1 + num_credits2 + num_credits3; return total_num_credits; }

C. Hundhausen, A. O’Fallon16 Problem Solving Example (9) Definition of compute_weighted_credits () double compute_weighted_credits (double grade_point1, double grade_point2, double grade_point3, int num_credits1, int num_credits2, int num_credits3) { double weighted_credits = 0.0; weighted_credits = (grade_point1 * num_credits1) + (grade_point2 * num_credits2) + (grade_point3 * num_credits3); return weighted_credits; }

C. Hundhausen, A. O’Fallon17 Problem Solving Example (10) Definition of compute_gpa () double compute_gpa (double weighted_credits, int total_num_credits) { double gpa = 0.0; gpa = weighted_credits / total_num_credits; return gpa; }

C. Hundhausen, A. O’Fallon18 Problem Solving Example (11) Definition of display_gpa () /* Outputs the calculated values to a file */ void display_gpa (FILE *outfile, double weighted_credits, int total_num_credits, double gpa) { fprintf (outfile, “Weighted Credits: %.2lf\n Total Credits: %d\n GPA: %.2lf\n", weighted_credits, total_num_credits, gpa); }

C. Hundhausen, A. O’Fallon19 File Opening Modes We have seen read (“r”), write (“w”), and append (“a”) modes Others exist, including: ◦ “r+”, opening an existing file for update (reading and writing) ◦ “w+”, create a file for update (if it already exists, discard current contents) ◦ “a+”, append: open or create a file for update; writing is done at the end of file Binary data and information may be read, written, and/or appended to a file by adding a “b” to the mode (“rb”, “wb”, “ab”, “rb+”, “wb+”, “ab+”)

C. Hundhausen, A. O’Fallon20 Common Programming Errors with Files Using the wrong file handle to refer to a file Opening a nonexistent file for reading Opening a file for reading or writing without the appropriate access rights Opening a file for writing when no disk space is available Opening a file for writing (“w”) when the users wants to preserve the previous contents of the file (“w” discards all contents of file)

C. Hundhausen, A. O’Fallon21 Random-Access Files (1) Individual records of a random-access file are normally fixed in length (i.e. student record information contains: name, id, age, gender, class standing, gpa, etc.) The exact location of a record relative to beginning of file may be calculated based on record key Fixed-length records enable inserting and updating of records without destroying other records

C. Hundhausen, A. O’Fallon22 Random-Access Files (2) Function fwrite () transfers a specified number of bytes (beginning at a specified location in memory) to a file Function fread () transfers a specified number of bytes from the location n of the file (specified by file position pointer) to an area in memory Function fseek () sets the file position pointer to a specific position in the file

C. Hundhausen, A. O’Fallon23 Looking to the Future I encourage you to revisit functions fread (), fwrite (), and fseek () once we have discussed arrays and pointers These functions allow for more efficient searching and updating of data in files