Download presentation
Presentation is loading. Please wait.
Published byCuthbert Houston Modified over 9 years ago
1
File Handling and Control Break Logic
2
Objectives In this chapter, you will learn about: Computer files Writing a program that reads from and/or writes to a file – Input file – Output file The data hierarchy Control break logic 2
3
Understanding Computer Files Random access memory (RAM) – aka Primary Storage Temporary storage Volatile storage (requires constant refreshing) – Example: store a value in a variable Permanent storage – aka Secondary Storage Non-volatile storage – Example: save a program to a USB drive 3
4
Understanding Computer Files File Concepts – A file is a collection of related data stored on a storage device – Types Computer Files – Text file (Notepad file) – Binary file (created by a user-written program) Word document Excel Workbook Access Database Music (MP3) file / Video (MP4) file PDF files – Characteristics (attributes) of a file Name Time and date of creation and last modification Size measured in bytes Application used to open it – Windows looks at the file extension to determine this 4
5
Organizing Files Folders (aka directories) – Container for storing related files – My Music, My Pictures, My Documents, Java, RAPTOR, Assignments Microsoft DOS used the term directory – directory / subdirectory Windows uses the term folder – folder / subfolder Path – Combination of the disk drive letter plus the complete hierarchy of directories in which a file resides – F:\CIS 103\Assignments\Assignment_7\PayrollData.dat – Put your program and any data files in the same folder – Do NOT work directly with files in a compressed folder! 5
6
Understanding the Data Hierarchy Data hierarchy – Describes the relationships between data components – Consists of: File[collection of records] Record[collection of fields] Field [collection of characters] letters, digits, etc. 6
7
Performing File Operations (pseudocode) Use data files in your programs – Declare a file InputFile: EmployeeData.txt – RAPTOR Redirect_Input("EmployeeData.txt") OutputFile: EmployeeReport.txt – RAPTOR Redirect_Output("EmployeeReport.txt") – Open a file - pseudocode open "EmployeeData.txt" as inputFile(file handle) open "EmployeeReport.txt" as outputFile(file handle) – Read from a file (get) - pseudocode get name [from inputFile] get address [from inputFile] get payRate [from inputFile] 7
8
Performing File Operations (continued) Figure 7-2 Reading three data items from a storage device into memory 8
9
– Write (put) Put name, address, payRate to outputReport single statement or multiple statements – Close close inputFile close outputFile RAPTOR closes files automatically 9 Performing File Operations (pseudocode)
10
Performing File Operations in Java Declare – public Scanner inputFile; – public PrintWriter outputFile; Open – inputFile = new Scanner( new File("EmployeeData.txt") ); – outputFile = new PrintWriter( “EmloyeeReport.txt" ); Read – name = inputFile.next(); //get a string delimited by whitespace – name = inputFile.nextLine(); //get rest of line as a string – payRate = inputFile.nextDouble(); – hoursWorked = inputFile.nextDouble(); 10
11
Performing File Operations in Java Write – outputFile.println(name); – formatted output outputFile.printf("%-15s %-40s %,10.2f%n", name, address, netPay); Close – inputFile.close(); – outputFile.close(); 11
12
Programming Logic & Design, Sixth Edition 12 Figure 7-3 Flowchart for a program that uses files and a priming read Priming read Don't usually need this in Java or RAPTOR! Regular read
13
Programming Logic & Design, Sixth Edition 13 Figure 7-3 Revised (no priming read) Flowchart for a program that uses a file Move regular read A priming read is not usually necessary in RAPTOR or Java Remove priming read
14
Programming Logic & Design, Sixth Edition 14 Figure 7-3 Pseudocode for a program that uses a file No priming read in housekeeping( )! Error in pseudocode…
15
Understanding Control Break Logic control break logic – Temporary “detour” in the logic of a program We will use control break logic to print a summary line – Create a report summary report only detail and summary report – To print a summary line you need to first accumulate all the relevant data. 15
16
Understanding Control Break Logic (continued) Control break program – Typically a change in the value of a variable initiates special actions or causes special processing to occur Hold field does not match input field – Single- or multi-level control breaks are possible – field / hold field Note: control break programs usually have a priming read; this is necessary to set up hold fields! 16
17
Understanding Control Break Logic (continued) Figure 7-4 A control break report with client totals for each state Clients by State Report 17 Control Break logic requires a file be sorted on one or more fields state is the control break field Hold field could be called oldState Note: File is sorted by city within state – a double level control break is possible.
18
Understanding Control Break Logic (continued) Examples of control break reports – employee report sorted by department number, with a new page started for each department – Instead of a summary line a page break is generated when the department number changes. – books for sale by category report sorted by category (such as reference or self-help), with a count following each category of book 18
19
Programming Logic & Design, Sixth Edition 19 Figure 7-5 Mainline logic and getReady() module for a program that produces a clients by state report Set up the hold field (oldState). The first record had to be read to set up the hold field. Priming read.
20
Programming Logic & Design, Sixth Edition 20 Figure 7-5 Mainline logic and getReady() module for a program that produces a report of clients within state report hold field declaration - oldState this variable will hold the state we are accumulating information for priming read initialize hold variable
21
Programming Logic & Design, Sixth Edition 21 Figure 7-6 The produceReport( ) and controlBreak( ) modules for a program that produces a report of clients within state normal processing for a detail and summary report
22
Programming Logic & Design, Sixth Edition 22 Figure 7-6 The produceReport() and controlBreak( ) modules for a program that produces a report of clients within state check for a control break normal processing Notice use of oldState here instead of state
23
23 Figure 7-7 The finishUp() module for a program that produces a report of clients within state
24
24 Performing Multiple-Level Control Breaks New Example Summary report: – group totals, not detail records city totals state totals grand total – contains summary information and optionally detail information Multiple-level control break: – breaks occur for more than one change in condition [multiple hold fields] Report of book sales by city and state [summary information for city and state ] Book Sales by city within state state major city minor Summary lines: City total State total Grand total
25
25 Performing Multiple-Level Control Breaks In this example, a control break occurs when: – Value of city variable changes[ oldCity ] – Value of state variable changes[ oldState ] Input file must be sorted by city within state When you detect a new city record print total for city When you detect a new state record print totals forcity and state When you detect end-of-file print totals for city, state, and grand total Use arrays to store book counts as well as control break fields IMHO: The use of arrays here is an additional unnecessary complication Check for a change in the state then for a change in the city Check highest to lowest
26
Programming Logic and Design, Fifth Edition, Comprehensive 26 Performing Multiple-Level Control Breaks cityBreak( ) module performs standard tasks: – Performs processing for previous group – Rolls up the current-level totals to next higher level – Resets current level’s totals to 0 – Performs processing for new group – Updates the control break field stateBreak( ) module does the same, starting with processing cityBreak( ) module you must check for a break in the state before checking for a break in the city!
27
Programming Logic and Design, Fifth Edition, Comprehensive 27 Performing Multiple-Level Control Breaks Main program checks for change in city and state variables [ checks for state change first… ] When city changes, city’s name and total are printed When state changes, state’s name and total are printed All city totals within a state print before state total for same state – Seems logical to check for change in city before state, but that would be incorrect logic! Must check for state change first
28
28 Performing Multiple-Level Control Breaks If two cities with the same name (in the same state) follow each other: – Program will not detect new city name Always check for a major-level control break first – if the records are sorted by city within state, a change in the state causes a major-level break If the records are sorted by city within state, then a change in the city causes a minor-level break Change in state implies a change in city – Even if the cities have the same name
29
29 Performing Multiple-Level Control Breaks Within each control break module, check if you need to: – Perform control break processing for the previous group(s) (if any) if the state changed, then you must perform a control break on the city – Perform any control break processing for current group – Roll up the current-level totals to next higher level – Reset the current-level totals to 0 – Update the control break field(s)
30
30 Figure 8-21 Sample portion of data for Book Sales report control break module would need to be called 3 times
31
31 Summary Control break: – a change in a variable’s value causes special actions to occur Control break field: – holds data from a previous record to compare to the current record Control break data can be used in a heading or footer Control break report prints summary lines and optionally detail lines
32
32 Summary (continued) For multiple-level control breaks, test for a major- level break before a minor-level break In a control break, check if lower-level breaks need to be processed
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.