Chapter 12 Text and Binary File Processing Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

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.
UNIT 15 File Processing.
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.
© 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.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
1 TDBA66, vt-04, Lecture Ch9 Text files  Text file contains ASCII-characters  New line and end-of-file are special characters in a text file Ex. 1 
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 8 - Characters and Strings Outline 8.1Introduction.
Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
Chapter 11 Structure and Union Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
Files Programs and data are stored on disk in structures called files Examples Turbo C++ - binary file Word binary file lab1.c - text file lab1.data.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
CMPE13 Cyrus Bazeghi Chapter 18 I/O in C. CMPE Standard C Library I/O commands are not included as part of the C language. Instead, they are part.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
A First Book of ANSI C Fourth Edition Chapter 10 Data Files.
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.
Chapter 13 Programming in the Large Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Chapter 18 I/O in C.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Text and Binary File Processing 程式設計 潘仁義 CCU COMM.
Chapter 8 : Binary Data Files1 Binary Data Files CHAPTER 8.
A. Abhari CPS1251 Input/Output Files, Review Text file: a named collection of characters newline, input stream, output stream This is first line. This.
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 12 Files (reference: Deitel ’ s chap 11) chap8.
CNG 140 C Programming (Lecture set 10) Spring Chapter 10 Data Files.
Text and Binary File Processing 程式設計 潘仁義 CCU COMM.
ME-2221 COMPUTER PROGRAMMING Lecture 18 FILE OPERATIONS Department of Mechanical Engineering A.H.M Fazle Elahi Khulna University of engineering & Technology.
Chapter 12 Files (reference: Deitel ’ s chap 11) chap8.
1 CSC103: Introduction to Computer and Programming Lecture No 28.
Files A collection of related data treated as a unit. Two types Text
CSCI N305: C Programming Copyright ©2006  Department of Computer & Information Science File Handling in C.
 2007 Pearson Education, Inc. All rights reserved. 1 C File Processing.
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.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic properties and characteristics of external files ❏ To.
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.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Chapter 18 I/O in C Original slides from Gregory Byrd, North Carolina State University Modified slides by C. Wilcox, Y. Malaiya Colorado State University.
Problem Solving and Program Design in C Chap. 11 Text and Binary File Processing Chow-Sing Lin.
11 C File Processing.
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
TMF1414 Introduction to Programming
Chapter 7 Text Input/Output Objectives
Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng.
Chapter 18 I/O in C.
Introduction to Computer Programming Lecture 18 Binary Files
File I/O.
CS111 Computer Programming
File Input/Output.
Chapter 11 – File Processing
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
INPUT & OUTPUT scanf & printf.
Text and Binary File Processing
Fundamental of Programming (C)
Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng.
Chapter 12: File I/O.
Chapter 11 Files chap8.
CSc 352 File I/O Saumya Debray Dept. of Computer Science
Presentation transcript:

Chapter 12 Text and Binary File Processing Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.12-2 Outline 12.1 INPUT/OUTPUT FILES: REVIEW AND FURTHER STUDY 12.2 BINARY FILES 12.3 SEARCHING A DATABASE CASE STUDY DATABASE INQUIRY 12.4 COMMON PROGRAMMING ERRORS

Copyright ©2004 Pearson Addison-Wesley. All rights reserved INPUT/OUTPUT FILES: REVIEW AND FURTHER STUDY text file –a named collection of characters saved in secondary storage input (output) stream –continuous stream of character codes representing textual input (or output) data stdin –system file pointer for keyboard’s input stream stdout, stderr –system file pointers for screen’s output stream

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.12-4 TABLE 12.1 Meanings of Common Escape Sequences Escape SequenceMeaning ‘\n’new line ‘\t’tab ‘\f’form feed (new page) ‘\r’return (go back to column 1 of current output line) ‘\b’backspace

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.12-5 TABLE 12.2 Placeholders for printf Format Strings PlaceholderUsed for Output of %ca single character %sa string %dan integer (in base 10) %oan integer (in base 8) %xan integer (in base 16) %fa floating-point number %ea floating-point number in scientific notation %Ea floating-point number in scientific notation %a single % sign

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.12-6 TABLE 12.4 Comparison of I/O with Standard Files and I/O with User-Defined File Pointers LineFunctions That Access stdin and stdout Functions That Can Access Any Text File 1scanf(“%d”, &num);fscanf(infilep,“%d”, &num); 2printf(“Number=%d\n”,num);fprintf(outfilep, “Number=%d\n”,num); 3ch=getchar();ch=getc(infilep); 4putchar(ch);putc(ch,outfilep);

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.12-7 Example 12.1 For security reasons, having a backup or duplicate copy of a file is a good idea, in case the original is lost. The program in Fig.12.1 copies each character in one file to backup file and allows the user to enter interactively both the name of the file copy and the name of the backup file.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.12-8 Figure 12.1 Program to Make a Backup Copy of a Text File

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.12-9 Figure 12.1 Program to Make a Backup Copy of a Text File (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure 12.2 Input and Output Streams for File Backup Program

Copyright ©2004 Pearson Addison-Wesley. All rights reserved BINARY FILES binary file –a file containing binary numbers that are the computer’s internal representation of each file component sizeof –operator that finds the number of bytes used for storage of a data type

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure 12.3 Creating a Binary File of Integers

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Function fread The stdio library includes an input function fread that is comparable to fwrite Function fread requires four arguments –Address of first memory cell to fill –Size of one value –Maximum number of elements to copy from the file into memory –File pointer to a binary file opened in mode “rb” using function fopen

Copyright ©2004 Pearson Addison-Wesley. All rights reserved SEARCHING A DATABASE database –a vast electronic file of information that can be quickly searched using subject headings or keywords

Copyright ©2004 Pearson Addison-Wesley. All rights reserved CASE STUDY: DATABASE INQUIRY(1/4) Step 1: Problem –Periphs Plus is a mail-order computer supply company that maintains its inventory as a computer file in order to facilitate answering questions regarding that database –Some questions of interest might be: What printer stands that cost less than $100 are available? What product has the code 5241? What types of data cartridges are available?

Copyright ©2004 Pearson Addison-Wesley. All rights reserved CASE STUDY: DATABASE INQUIRY(2/4) Step 2: Analysis –Problem Inputs search_params_t params; char inv_filename[STR_SIZ] –Problem Output All products that satisfy the search

Copyright ©2004 Pearson Addison-Wesley. All rights reserved CASE STUDY: DATABASE INQUIRY(3/4) Step 3: Design (Figure 12.4) –Algorithm 1. Open inventory file 2. Get search parameters 3. Display all products that satisfy the search parameters

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure 12.4 Structure Chart for Database Inquiry Problem

Copyright ©2004 Pearson Addison-Wesley. All rights reserved CASE STUDY: DATABASE INQUIRY(4/4) Step 4: Implementation (Figure 12.5) Step 5: Testing

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure 12.5 Outline and Function main for Database Inquiry Program

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure 12.5 Outline and Function main for Database Inquiry Program (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure 12.5 Outline and Function main for Database Inquiry Program (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Design of The Function Subprograms (1/3) Function get_params must first initialize the search parameters to allow the widest search possible and then let the user change some parameters to narrow the search

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure 12.6 Structure Chart for get_params

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Design of The Function Subprograms (2/3) Local Variables for get_grams –search_params_t params –char choice Algorithm for get_grams 1. Initialize params to permit widest possible search 2. Display menu and get response to store in choice 3. Repeat while choice is not ‘q’ 4. Select appropriate prompt and get new parameter value 5. Display menu and get response to store in choice 6. Return search parameters

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure 12.7 Structure Chart for display_match

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Design of The Function Subprograms (3/3) Local Variables for display_match –product_t next_prod –int no_matches Algorithm for display_match 1. Initialize no_matches to true(1) 2. Advance to the first record whose stock number is within range 3. while the current stock number is still in range repeat 4. if the search parameters match 5. Display the product and set no_matches to false(0) 6. Get the next product record 7. if there are no matches 8. Print a no products available message

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure 12.8 Functions display_match, menu_choose, and match

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure 12.8 Functions display_match, menu_choose, and match (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure 12.8 Functions display_match, menu_choose, and match (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure 12.8 Functions display_match, menu_choose, and match (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved COMMON PROGRAMMING ERRORS (1/2) Remember to declare a file pointer variable (type FILE *) for each file you want to process fscanf, fprintf, getc and putc must be used for text I/O only fread and fwrite are applied exclusively to binary files fscanf, fprintf and getc take the file pointer as their first argument putc, fread and fwrite take the file pointer as their last argument

Copyright ©2004 Pearson Addison-Wesley. All rights reserved COMMON PROGRAMMING ERRORS (2/2) Opening a file for output by calling fopen with a second argument of “w” or “wb” typically results in a loss of any existing file whose name matches the first argument Binary files cannot be created, viewed, or modified using an editor or word processor program

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Chapter Review Text files are continuous streams of characters codes that can be viewed as broken into lines by the newline character Binary files permit storage of information using a computer’s internal data format –Neither time nor accuracy is lost through conversion of values transferred between main and secondary storage

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Question?