25
1 CS 106 Computing Fundamentals II Chapter 67 “Working With Files” Herbert G. Mayer, PSU CS Herbert G. Mayer, PSU CS status 6/24/2013 status 6/24/2013 Initial content copied verbatim from Initial content copied verbatim from CS 106 material developed by CS 106 material developed by CS professors: Cynthia Brown & Robert Martin CS professors: Cynthia Brown & Robert Martin

1 CS 106 Computing Fundamentals II Chapter 67 “Working With Files” Herbert G. Mayer, PSU CS status 6/24/2013 Initial content copied verbatim from CS 106

Embed Size (px)

Citation preview

Page 1: 1 CS 106 Computing Fundamentals II Chapter 67 “Working With Files” Herbert G. Mayer, PSU CS status 6/24/2013 Initial content copied verbatim from CS 106

1

CS 106Computing Fundamentals II

Chapter 67“Working With Files”

Herbert G. Mayer, PSU CSHerbert G. Mayer, PSU CSstatus 6/24/2013status 6/24/2013

Initial content copied verbatim fromInitial content copied verbatim fromCS 106 material developed byCS 106 material developed by

CS professors: Cynthia Brown & Robert MartinCS professors: Cynthia Brown & Robert Martin

Page 2: 1 CS 106 Computing Fundamentals II Chapter 67 “Working With Files” Herbert G. Mayer, PSU CS status 6/24/2013 Initial content copied verbatim from CS 106

2

Syllabus Sources of DataSources of Data

Data File FormatsData File Formats

Text FilesText Files

File NumberFile Number

Reading a Single ItemReading a Single Item

Reading Multiple ItemsReading Multiple Items

Read n CharactersRead n Characters

Page 3: 1 CS 106 Computing Fundamentals II Chapter 67 “Working With Files” Herbert G. Mayer, PSU CS status 6/24/2013 Initial content copied verbatim from CS 106

3

Sources of Data

Adding data to a spreadsheet can be done in several Adding data to a spreadsheet can be done in several ways, including:ways, including: Type it in piece by piece Read it from a file Link to a database

In this presentation we focus on reading data from a fileIn this presentation we focus on reading data from a file

Page 4: 1 CS 106 Computing Fundamentals II Chapter 67 “Working With Files” Herbert G. Mayer, PSU CS status 6/24/2013 Initial content copied verbatim from CS 106

4

Data File Formats

• There are some standard data formats you can easily There are some standard data formats you can easily read into Excelread into Excel

• Perhaps the most common one uses comma Perhaps the most common one uses comma separated values (csv)separated values (csv)

• The idea is that the data is in lines of text, one line for The idea is that the data is in lines of text, one line for each row, with the entries for each column separated each row, with the entries for each column separated by commasby commas

• Many kinds of software allow you to export data in Many kinds of software allow you to export data in this format with the idea that you will then read it into this format with the idea that you will then read it into ExcelExcel

Page 5: 1 CS 106 Computing Fundamentals II Chapter 67 “Working With Files” Herbert G. Mayer, PSU CS status 6/24/2013 Initial content copied verbatim from CS 106

5

Text Files

• Many of the files produced by software products have Many of the files produced by software products have extra information in them besides just textextra information in them besides just text

• For example, Word documents contain the For example, Word documents contain the information about fonts, bold, italics, bullets and information about fonts, bold, italics, bullets and indenting, etc., along with the actual textindenting, etc., along with the actual text

• The same is true for Excel files, whether .xlsx or .xlsmThe same is true for Excel files, whether .xlsx or .xlsm

• A comma separated values file (.csv file extension) is A comma separated values file (.csv file extension) is basically just a text file. Using .csv rather than .txt basically just a text file. Using .csv rather than .txt lets Excel know it’s in a format that can be read lets Excel know it’s in a format that can be read directly into a spreadsheetdirectly into a spreadsheet

Page 6: 1 CS 106 Computing Fundamentals II Chapter 67 “Working With Files” Herbert G. Mayer, PSU CS status 6/24/2013 Initial content copied verbatim from CS 106

6

The Input Function

• The Input function is used to read data from filesThe Input function is used to read data from files

• There are several ways to use it. Examples are in the There are several ways to use it. Examples are in the InputDemo workbook; you should try them out. InputDemo workbook; you should try them out.

• It’s especially interesting to try versions of Input on It’s especially interesting to try versions of Input on files not designed for them and see what happens files not designed for them and see what happens

Page 7: 1 CS 106 Computing Fundamentals II Chapter 67 “Working With Files” Herbert G. Mayer, PSU CS status 6/24/2013 Initial content copied verbatim from CS 106

7

Setting up to read…

• The first step is to open a file for reading.The first step is to open a file for reading.

• Excel has a nice dialog box that lets the user browse Excel has a nice dialog box that lets the user browse for a file. for a file.

• The next slide shows the relevant code from the The next slide shows the relevant code from the InputDemoInputDemo

Page 8: 1 CS 106 Computing Fundamentals II Chapter 67 “Working With Files” Herbert G. Mayer, PSU CS status 6/24/2013 Initial content copied verbatim from CS 106

8

Code to open a file.. And close it

fName = Application.GetOpenFilename()fName = Application.GetOpenFilename()

IfIf fName = fName = False Then False Then 'user cancelled'user cancelled

Exit Sub Exit Sub

End IfEnd If

OpenOpen fName fName For Input Access Read As For Input Access Read As #1#1

<program body><program body>

CloseClose #1 #1 'close the file you opened'close the file you opened

Page 9: 1 CS 106 Computing Fundamentals II Chapter 67 “Working With Files” Herbert G. Mayer, PSU CS status 6/24/2013 Initial content copied verbatim from CS 106

9

File Number

• Where we used #1, you can use any numberWhere we used #1, you can use any number

• Using the number lets you have several files open Using the number lets you have several files open and choose which ones to read and/or write toand choose which ones to read and/or write to

• Be sure to close any files you open in your program Be sure to close any files you open in your program when you are done with them to avoid file corruptionwhen you are done with them to avoid file corruption

Page 10: 1 CS 106 Computing Fundamentals II Chapter 67 “Working With Files” Herbert G. Mayer, PSU CS status 6/24/2013 Initial content copied verbatim from CS 106

10

Reading a Whole Line

One way to use Input is to read a whole line at a time. One way to use Input is to read a whole line at a time. Here is the code for that version:Here is the code for that version:

Line Input Line Input #1, oneLine #1, oneLine

Here oneLine is a string variableHere oneLine is a string variable

Page 11: 1 CS 106 Computing Fundamentals II Chapter 67 “Working With Files” Herbert G. Mayer, PSU CS status 6/24/2013 Initial content copied verbatim from CS 106

11

Whole Line Text File

George WashingtonGeorge Washington

John AdamsJohn Adams

Thomas JeffersonThomas Jefferson

John Quincy AdamsJohn Quincy Adams

Abraham LincolnAbraham Lincoln

Page 12: 1 CS 106 Computing Fundamentals II Chapter 67 “Working With Files” Herbert G. Mayer, PSU CS status 6/24/2013 Initial content copied verbatim from CS 106

12

Whole Line Output

The program prints a linesaying “done” if it finishes normally

Page 13: 1 CS 106 Computing Fundamentals II Chapter 67 “Working With Files” Herbert G. Mayer, PSU CS status 6/24/2013 Initial content copied verbatim from CS 106

13

Reading a Single Item

• To read multiple single items, you separate them from To read multiple single items, you separate them from one another with commasone another with commas

• Excel is primed to expect comma delimited valuesExcel is primed to expect comma delimited values

• It also regards the end of a line as a separatorIt also regards the end of a line as a separator

• In code samples we use In code samples we use oneItemoneItem, a string variable, a string variable

InputInput #1, oneItem #1, oneItem

Page 14: 1 CS 106 Computing Fundamentals II Chapter 67 “Working With Files” Herbert G. Mayer, PSU CS status 6/24/2013 Initial content copied verbatim from CS 106

14

Single Item Text File

George, WashingtonGeorge, Washington

John, AdamsJohn, Adams

Thomas, JeffersonThomas, Jefferson

John, Quincy, AdamsJohn, Quincy, Adams

Abraham, LincolnAbraham, Lincoln

Page 15: 1 CS 106 Computing Fundamentals II Chapter 67 “Working With Files” Herbert G. Mayer, PSU CS status 6/24/2013 Initial content copied verbatim from CS 106

15

Single Item Output

Page 16: 1 CS 106 Computing Fundamentals II Chapter 67 “Working With Files” Herbert G. Mayer, PSU CS status 6/24/2013 Initial content copied verbatim from CS 106

16

Read Multiple Items

• To do this, you provide more than one variable as a To do this, you provide more than one variable as a targettarget

• If your file does not have the right number of items, If your file does not have the right number of items, your program will terminate abnormallyyour program will terminate abnormally

• Here’s the code for reading two items at once: (ours Here’s the code for reading two items at once: (ours are both strings but you can use other types if you are both strings but you can use other types if you know your data is numbers, for example)know your data is numbers, for example)

InputInput #1, item1, item2 #1, item1, item2

Page 17: 1 CS 106 Computing Fundamentals II Chapter 67 “Working With Files” Herbert G. Mayer, PSU CS status 6/24/2013 Initial content copied verbatim from CS 106

17

Two Item Text File

George, WashingtonGeorge, Washington

John, AdamsJohn, Adams

Thomas, JeffersonThomas, Jefferson

Andrew, JacksonAndrew, Jackson

Abraham, LincolnAbraham, Lincoln

Page 18: 1 CS 106 Computing Fundamentals II Chapter 67 “Working With Files” Herbert G. Mayer, PSU CS status 6/24/2013 Initial content copied verbatim from CS 106

18

Two Item Result

Page 19: 1 CS 106 Computing Fundamentals II Chapter 67 “Working With Files” Herbert G. Mayer, PSU CS status 6/24/2013 Initial content copied verbatim from CS 106

19

Odd number result

Here we used our one time file, which has an odd number of items. The program terminated abnormally when it couldn’t find an item in the last read

Page 20: 1 CS 106 Computing Fundamentals II Chapter 67 “Working With Files” Herbert G. Mayer, PSU CS status 6/24/2013 Initial content copied verbatim from CS 106

20

Read N Characters

• You can also read a block of N charactersYou can also read a block of N characters

• This may behave in an unexpected way since there This may behave in an unexpected way since there are some “invisible” characters in text files, such as are some “invisible” characters in text files, such as carriage return and line feed.carriage return and line feed.

• Here’s the code; note the different syntax. Variable Here’s the code; note the different syntax. Variable chars is of type String. chars is of type String.

chars = chars = InputInput(N, #1)(N, #1)

Page 21: 1 CS 106 Computing Fundamentals II Chapter 67 “Working With Files” Herbert G. Mayer, PSU CS status 6/24/2013 Initial content copied verbatim from CS 106

21

The Data File

This file ends with a return, which you can’t see. But look at the results…

Page 22: 1 CS 106 Computing Fundamentals II Chapter 67 “Working With Files” Herbert G. Mayer, PSU CS status 6/24/2013 Initial content copied verbatim from CS 106

22

Reading one character at a time

Lines 21 and 22 contain the carriage return and line feed characters

Page 23: 1 CS 106 Computing Fundamentals II Chapter 67 “Working With Files” Herbert G. Mayer, PSU CS status 6/24/2013 Initial content copied verbatim from CS 106

23

Two at a time:

Now line 11 has the carriage return and line feed characters

Page 24: 1 CS 106 Computing Fundamentals II Chapter 67 “Working With Files” Herbert G. Mayer, PSU CS status 6/24/2013 Initial content copied verbatim from CS 106

24

Four at a Time

Abnormal termination. The number of visible characters is 16, but the actual number is 18.

Page 25: 1 CS 106 Computing Fundamentals II Chapter 67 “Working With Files” Herbert G. Mayer, PSU CS status 6/24/2013 Initial content copied verbatim from CS 106

25

Why Read Characters?

• You might want to write code that picks out only You might want to write code that picks out only certain kinds of items from the text, for example. certain kinds of items from the text, for example.

• You can use string functions along with You can use string functions along with concatenation to build up things like a catalog of concatenation to build up things like a catalog of words in a text while ignoring punctuationwords in a text while ignoring punctuation

• Usually, though, reading one item at a time is the Usually, though, reading one item at a time is the right approachright approach