copyright © 2010 pearson education, inc. publishing as pearson addison-wesley starting out with...

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

Upload: peter-cummings

Post on 19-Jan-2018

222 views

Category:

Documents


0 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Starting Out with Programming Logic & Design

Second Edition

by Tony Gaddis

Chapter 10:Files

Page 2: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-2

Chapter Topics10.1 Introduction to File Input and Output10.2 Using Loops to Process Files10.3 Using Files and Arrays10.4 Processing Records10.5 Control Break Logic

Page 3: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-3

10.1 Introduction to File Input and OutputWhen 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

Page 4: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-4

10.1 Introduction to File Input and Output

Three steps must take place for file interaction1. Open the file. An output file means creating and

preparing it for output; an input file means opening a file and preparing 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

Page 5: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-5

10.1 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

Page 6: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-6

10.1 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

Page 7: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-7

10.1 Introduction to File Input and Output

Create a file and writing data to it1. 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

Page 8: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-8

10.1 Introduction to File Input and Output

3. Files must be openedOpen customerFile “customers.dat”

4. Data can then be written to a fileWrite customerFile = “Charles Pace”

orDeclare String name = “Charles Pace”Write customerFile name

5. Closing a fileClose customerFile

Page 9: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-9

10.1 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

Page 10: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-10

10.1 Introduction to File Input and OutputReading 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

Page 11: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-11

10.1 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

Page 12: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-12

10.2 Using Loops to Process FilesLoops can be used to enter large amounts of data

For counter =1 To numDaysDisplay “Enter the sales for day #”, counterInput salesWrite salesFile sales //writes to the file

End For

Loop could also be used to read large amounts of dataWhile NOT eof(salesFile)

Read salesFile salesDisplay currencyFormat(sales)

End While

Page 13: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-13

10.3 Using Files and ArraysFiles 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

Page 14: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-14

10.4 Processing RecordsData 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 10-18 Fields in a record

Page 15: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-15

10.4 Processing RecordsWriting 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

Page 16: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-16

10.5 Control Break LogicControl 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

Page 17: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-17

10.5 Control Break LogicIf lines==24 Then

Display “Press any key to continue…”Input Set lines = 0 // resets the counter

End If

Figure 10-27 Pausing output after 24 items are displayed

Page 18: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

End of Chapter Review

Multiple Choice questions 1 – 15 pages 407-408

1-18

Page 19: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Question 1

A file that data is written to is known as a(n)a.input fileb.output filec.sequential access filed.binary file

1-19

Page 20: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Question 1

A file that data is written to is known as a(n)a.input fileb.output filec.sequential access filed.binary file

1-20

Page 21: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Question 2

A file that data is read from is know as a(n)a.input fileb.output filec.sequential access filed.binary file

1-21

Page 22: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Question 2

A file that data is read from is know as a(n)a.input fileb.output filec.sequential access filed.binary file

1-22

Page 23: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Question 3

Before a file can be used by a program , it must bea.formattedb.encryptedc.closedd.opened

1-23

Page 24: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Question 3

Before a file can be used by a program , it must bea.formattedb.encryptedc.closedd.opened

1-24

Page 25: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Question 4

When a program is finished using a file, it should do this.a.erase the fileb.open the filec.close the filed.encrypt the file

1-25

Page 26: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Question 4

When a program is finished using a file, it should do this.a.erase the fileb.open the filec.close the filed.encrypt the file

1-26

Page 27: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Question 5

The contents of this type of file can be viewed in an editor such as Notepad.a.text fileb.binary filec.English filed.human-readable file

1-27

Page 28: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Question 5

The contents of this type of file can be viewed in an editor such as Notepad.a.text fileb.binary filec.English filed.human-readable file

1-28

Page 29: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Question 6

This type of file contains data that has not been converted to text.a.text fileb.binary filec.Unicode filed.symbolic file

1-29

Page 30: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Question 6

This type of file contains data that has not been converted to text.a.text fileb.binary filec.Unicode filed.symbolic file

1-30

Page 31: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Question 7

When working with this type of file, you access its data from the beginning of the file to the end of the file.a.ordered accessb.binary accessc.direct accessd.sequential access

1-31

Page 32: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Question 7

When working with this type of file, you access its data from the beginning of the file to the end of the file.a.ordered accessb.binary accessc.direct accessd.sequential access

1-32

Page 33: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Question 8

When working with this type of file, you can jump directly to any piece of data in the file without reading the data that comes before it.a.ordered accessb.binary accessc.direct accessd.sequential access

1-33

Page 34: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Question 8

When working with this type of file, you can jump directly to any piece of data in the file without reading the data that comes before it.a.ordered accessb.binary accessc.direct accessd.sequential access

1-34

Page 35: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Question 9

This is a small “holding section” in memory that many systems write data to before writing the data to a file.a.bufferb.variablec.virtual filed.temporary file

1-35

Page 36: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Question 9

This is a small “holding section” in memory that many systems write data to before writing the data to a file.a.bufferb.variablec.virtual filed.temporary file

1-36

Page 37: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Question 10

This is a character or a set of characters that marks the end of a piece of data.a.median valueb.delimiterc.boundary markerd.EOF marker

1-37

Page 38: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Question 10

This is a character or a set of characters that marks the end of a piece of data.a.median valueb.delimiterc.boundary markerd.EOF marker

1-38

Page 39: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Question 11

This is a character or set of characters that marks the end of a file.a.median valueb.delimiterc.boundary markerd.EOF marker

1-39

Page 40: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Question 11

This is a character or set of characters that marks the end of a file.a.median valueb.delimiterc.boundary markerd.EOF marker

1-40

Page 41: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Question 12

This marks the location of the next item that will be read from a file.a.input positionb.delimiterc.pointerd.read position

1-41

Page 42: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Question 12

This marks the location of the next item that will be read from a file.a.input positionb.delimiterc.pointerd.read position

1-42

Page 43: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Question 13

When a file is opened in this mode, data will be written at the end of the file’s existing contents.a.output modeb.append modec.backup moded.read-only mode

1-43

Page 44: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Question 13

When a file is opened in this mode, data will be written at the end of the file’s existing contents.a.output modeb.append modec.backup moded.read-only mode

1-44

Page 45: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Question 14

The expression NOT eof(myFile) is equivalent toa.eof(myFile) == Trueb.eof(myFile) c.eof(myFile) == Falsed.eof(myFile) < 0

1-45

Page 46: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Question 14

The expression NOT eof(myFile) is equivalent toa.eof(myFile) == Trueb.eof(myFile) c.eof(myFile) == Falsed.eof(myFile) < 0

1-46

Page 47: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Question 15

This is a single piece of data within a record.a.fieldb.variablec.delimiterd.subrecord

1-47

Page 48: Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming…

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Question 15

This is a single piece of data within a record.a.fieldb.variablec.delimiterd.subrecord

1-48