Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 Chapter 12 Text and Binary File Processing Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

2 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

3 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.12-3 12.1 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

4 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

5 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

6 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);

7 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.

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

9 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)

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

11 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.12-11 12.2 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

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

13 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.12-13 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

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

15 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.12-15 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?

16 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.12-16 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

17 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.12-17 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

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

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

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

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

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

23 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.12-23 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

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

25 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.12-25 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

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

27 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.12-27 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

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

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

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

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

32 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.12-32 12.4 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

33 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.12-33 12.4 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

34 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.12-34 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

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


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

Similar presentations


Ads by Google