Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.

Slides:



Advertisements
Similar presentations
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Advertisements

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
File Handling Advanced Higher Programming. What is a file? Up until now, any stored data within a program is lost when the program closes. A file is a.
Files Organisation sequential files. Readings u Schneider Chapter 8 u Shelly Cashman to 9.14; to 9.11 u Meyer to 2-37; 1995.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18 Indexing Structures for Files.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
An Introduction to Programming with C++ Fifth Edition Chapter 13 Sequential Access Files.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition by Tony Gaddis, Judy Walters,
Chapter 2: Input, Processing, and Output
COS 381 Day 22. Agenda Questions?? Resources Source Code Available for examples in Text Book in Blackboard
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Part 1 Conditionals and Loops.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Topics Introduction Hardware and Software How Computers Store Data
Programming Logic and Design Seventh Edition
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Chapter 1: Introduction to Computers and Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 1 Introduction to Computers and Programming.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 6: Sequential Data Files.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 3 Simple.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
C++ for Engineers and Scientists Second Edition Chapter 8 I/O File Streams and Data Files.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 7 Files.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
5 1 Data Files CGI/Perl Programming By Diane Zak.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 13 File Input and.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer.
Copyright © 2012 Pearson Education, Inc. Chapter 5 Loops, File, and Random Numbers.
Using Text Files in Excel File I/O Methods. Working With Text Files A file can be accessed in any of three ways: –Sequential access: By far the most common.
Pascal Programming Today Chapter 11 1 Chapter 11.
13-1 Sequential File Processing Chapter Chapter Contents Overview of Sequential File Processing Sequential File Updating - Creating a New Master.
Files Tutor: You will need ….
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 5 Repetition.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley What did we learn so far? 1.Computer hardware and software 2.Computer experience.
Files in Python The Basics. Why use Files? Very small amounts of data – just hardcode them into the program A few pieces of data – ask the user to input.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Chapter 14: Sequential Access Files
Chapter 6: Sequential Data Files
Topics Introduction Hardware and Software How Computers Store Data
Starting Out with Programming Logic & Design
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
Programming Logic and Design Fourth Edition, Comprehensive
Topics Introduction Hardware and Software How Computers Store Data
Topics Introduction to File Input and Output
Topics Introduction to File Input and Output
Presentation transcript:

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis Chapter 10: Files

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-2 Chapter Topics 10.1 Introduction to File Input and Output 10.2 Using Loops to Process Files 10.3 Using Files and Arrays 10.4 Processing Records 10.5 Control Break Logic

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Introduction to File Input and Output When a program needs to save data for later use, it writes the data in a file and can be used later –In previous programs you wrote, the data was stored in variables –File input and output can interact with various types of applications Word processors Image editors Spreadsheets Games Web browsers

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Introduction to File Input and Output Three steps must take place for file interaction 1.Open the file. An output file means creating and prepare it for output; an input file means opening a file and prepare it for reading data from 2.Process the file. Writes data to the file or reads data from the file 3.Close the file. Must be done to disconnect it from the program

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Introduction to File Input and Output Types of files include text and binary –A text file contains data that has been encoded as text, using ASCII or Unicode Even numbers in this type of file are stored as text –A binary file contains data that has not been converted to text A text editor is need to view the contents of a binary file

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Introduction to File Input and Output Files can be accessed in two methods –Sequential access Data is accessed from the beginning to the end All data must be read –Direct access (aka random access) Any piece of data can be accessed without reading the data that comes before or after it –This chapter will focus on sequential access files

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Introduction to File Input and Output Create a file and writing data to it 1.Files that are created should be given a name with an appropriate file extension customers.dat where.dat represents general data 2.Must also create an internal name that is similar to a variable name Declare OutputFile customerFile –OutputFile indicates the mode in which the file will be used –customerFile is the internal name used to work with the file

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Introduction to File Input and Output 3. Files must be opened Open customerFile “customers.dat” 4. Data can then be written to a file Write customerFile = “Charles Pace” or Declare String name = “Charles Pace” Write customerFile name 5. Closing a file Close customerFile

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Introduction to File Input and Output Delimiters and EOF Marker –A delimiter is a predefined character or set of characters that marks the end of each piece of data It separates the different items stored in a file –An End of File (EOF) marker is a special character or set of characters written to the end of a file It indicates the file’s contents end

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Introduction to File Input and Output Reading data from a file 1. An internal variable must first be declared Declare InputFile inventoryFile –InputFile indicates the mode in which the file will be used –inventoryFile is the internal name used to work with the file 2.The file can then be opened –Open inventoryFile “inventory.dat” 3.Data can then be read –Read inventoryFile itemName 4.And finally closed –Close inventoryFile

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Introduction to File Input and Output The append mode –In addition to read and write mode, append mode also exists to add data to a file that also exists –If the file already exists, it will not be erased –If the file does not exist, it will be created –When data is written to the file, it will be written to the end of the file Declare OutputFile AppendMode myFile

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Using Loops to Process Files Loops can be used to enter large amounts of data For counter =1 To numDays Display “Enter the sales for day #”, counter Input sales Write salesFile sales //writes to the file End For Loop could also be used to read large amounts of data While NOT eof(salesFile) Read salesFile sales Display currencyFormat(sales) End While

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Using Files and Arrays Files and arrays can be used together –The contents of an array can be saved to a file Open the file Use a loop to step through each element of the array Write the contents to a file on each iteration –The contents of a file can be read into an array Open the file Use a loop to read each item from the file Store each item in an array element

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Processing Records Data that is stored in a file is frequently organized in records –A record is a complete set of data about an item –A field is a single piece of data within a record Figure Fields in a record

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Processing Records Writing Records –An entire record is done using a single Write statement Write employeeFile name, idNumber, department Reading Records –Done in a similar fashion Read employeeFile name, idNumber, department Algorithms can also be used for adding records to a file, searching for a specific record(s), modifying a record, and/or deleting a record

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Control Break Logic Control break logic interrupts a program’s regular processing to perform a different action when a control variable’s value changes or the variable acquires a specific value –One example is the use of a line counter to pause the program before the information being displayed goes out of view –This can be done with an if statement

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Control Break Logic If lines==24 Then Display “Press any key to continue…” Input Set lines = 0 // resets the counter End If Figure Pausing output after 24 items are displayed