File Handling and Control Break Logic. Objectives In this chapter, you will learn about: Computer files Writing a program that reads from and/or writes.

Slides:



Advertisements
Similar presentations
Chapter 2: Modularization
Advertisements

Chapter 9: Advanced Array Manipulation
Programming Logic and Design Fourth Edition, Introductory
Programming Logic and Design, Second Edition, Comprehensive
Chapter 7: Control Breaks Programming Logic and Design, Third Edition Comprehensive.
Programming Logic and Design, Third Edition Comprehensive
Programming Logic and Design Fourth Edition, Introductory
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Writing a Complete Program
Chapter 1- Visual Basic Schneider1 Chapter 1 An Introduction to Computers and Visual Basic.
Modules, Hierarchy Charts, and Documentation
Chapter 8: I/O Streams and Data Files. In this chapter, you will learn about: – I/O file stream objects and functions – Reading and writing character-based.
Guide To UNIX Using Linux Third Edition
Programming Logic and Design Fourth Edition, Comprehensive
General Algorithms for Common Business Problems
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 1 Introduction.
Programming Logic and Design Fourth Edition, Introductory
MIS316 – BUSINESS APPLICATION DEVELOPMENT – Chapter 14 – Files and Streams 1Microsoft Visual C# 2012, Fifth Edition.
Database Software Application
Microsoft Office Illustrated Fundamentals Unit B: Understanding File Management.
Chapter Introduction to Computers and Programming 1.
1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming.
Chapter 7 Working with Files.
Computer Systems Week 10: File Organisation Alma Whitfield.
Computer Literacy BASICS: A Comprehensive Guide to IC 3, 5 th Edition Lesson 3 Windows File Management 1 Morrison / Wells / Ruffolo.
Topics Introduction Hardware and Software How Computers Store Data
Chapter 07 Control Breaks.
10- 1 Chapter 10. To familiarize you with  Main types of computer-generated reports  Techniques used for efficient printing of group reports and control.
Programming Logic and Design Seventh Edition
CIS Computer Programming Logic
Files and Streams 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Chapter 1: Introduction to Computers and Programming.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Chapter 11 File Systems and Directories. 2 File Systems File: A named collection of related data. File system: The logical view that an operating system.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
Chapter 9 I/O Streams and Data Files
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
6 Chapter 61 Looping Programming Logic and Design, Second Edition, Comprehensive 6.
An Object-Oriented Approach to Programming Logic and Design Chapter 1 An Overview of Computers and Logic.
General Algorithms for Common Business Problems Simple Program Design Third Edition A Step-by-Step Approach 10.
5 1 Data Files CGI/Perl Programming By Diane Zak.
Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
Gold – Crystal Reports Introductory Course Cortex User Group Meeting New Orleans – 2011.
Files Tutor: You will need ….
Chapter 1: Overview of SAS System Basic Concepts of SAS System.
Control Break Processing
COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)
Chapter 13.3: Databases Invitation to Computer Science, Java Version, Second Edition.
Computer and Programming. Computer Basics: Outline Hardware and Memory Programs Programming Languages and Compilers.
Chapter 11: Sequential File Merging, Matching, and Updating Programming Logic and Design, Third Edition Comprehensive.
11 Chapter 111 Sequential File Merging, Matching, and Updating Programming Logic and Design, Second Edition, Comprehensive 11.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
Java Programming, Second Edition Chapter Sixteen File Input and Output.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
SVBIT SUBJECT:- Operating System TOPICS:- File Management
Computer Literacy BASICS: A Comprehensive Guide to IC 3, 5 th Edition Lesson 3 Windows File Management 1 Morrison / Wells / Ruffolo.
Chapter 1: Introduction to Computers and Programming
Topics Introduction Hardware and Software How Computers Store Data
Week 12 Option 3: Database Design
Chapter 13: File Input and Output
Chapter 1: Introduction to Computers and Programming
Programming Logic and Design Eighth Edition
Programming Logic and Design Fourth Edition, Comprehensive
Topics Introduction Hardware and Software How Computers Store Data
Writing a Complete Program
Programming Logic and Design Fifth Edition, Comprehensive
Microsoft Office Illustrated Fundamentals
Chapter 4: Writing and Designing a Complete Program
Chapter 1: Introduction to Computers and Programming
Presentation transcript:

File Handling and Control Break Logic

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

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

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

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

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

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

Performing File Operations (continued) Figure 7-2 Reading three data items from a storage device into memory 8

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

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

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

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

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

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…

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

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

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.

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

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.

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

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

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 Figure 7-7 The finishUp() module for a program that produces a report of clients within state

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

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!

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 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 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 Figure 8-21 Sample portion of data for Book Sales report control break module would need to be called 3 times

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