Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 material developed by CS professors: Cynthia Brown & Robert Martin

2 2 Syllabus Sources of Data Sources of Data Data File Formats Data File Formats Text Files Text Files File Number File Number Reading a Single Item Reading a Single Item Reading Multiple Items Reading Multiple Items Read n Characters Read n Characters

3 3 Sources of Data Adding data to a spreadsheet can be done in several 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 file

4 4 Data File Formats There are some standard data formats you can easily read into ExcelThere are some standard data formats you can easily read into Excel Perhaps the most common one uses comma separated values (csv)Perhaps the most common one uses comma separated values (csv) The idea is that the data is in lines of text, one line for each row, with the entries for each column separated by commasThe idea is that the data is in lines of text, one line for each row, with the entries for each column separated by commas Many kinds of software allow you to export data in this format with the idea that you will then read it into ExcelMany kinds of software allow you to export data in this format with the idea that you will then read it into Excel

5 5 Text Files Many of the files produced by software products have extra information in them besides just textMany of the files produced by software products have extra information in them besides just text For example, Word documents contain the information about fonts, bold, italics, bullets and indenting, etc., along with the actual textFor example, Word documents contain the information about fonts, bold, italics, bullets and indenting, 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 basically just a text file. Using.csv rather than.txt lets Excel know it’s in a format that can be read directly into a spreadsheetA comma separated values file (.csv file extension) is basically just a text file. Using.csv rather than.txt lets Excel know it’s in a format that can be read directly into a spreadsheet

6 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 InputDemo workbook; you should try them out.There are several ways to use it. Examples are in the InputDemo workbook; you should try them out. It’s especially interesting to try versions of Input on files not designed for them and see what happensIt’s especially interesting to try versions of Input on files not designed for them and see what happens

7 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 for a file.Excel has a nice dialog box that lets the user browse for a file. The next slide shows the relevant code from the InputDemoThe next slide shows the relevant code from the InputDemo

8 8 Code to open a file.. And close it fName = Application.GetOpenFilename() If fName = False Then 'user cancelled Exit Sub Exit Sub End If Open fName For Input Access Read As #1 Close #1 'close the file you opened

9 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 and choose which ones to read and/or write toUsing the number lets you have several files open and choose which ones to read and/or write to Be sure to close any files you open in your program when you are done with them to avoid file corruptionBe sure to close any files you open in your program when you are done with them to avoid file corruption

10 10 Reading a Whole Line One way to use Input is to read a whole line at a time. Here is the code for that version: Line Input #1, oneLine Line Input #1, oneLine Here oneLine is a string variable

11 11 Whole Line Text File George Washington John Adams Thomas Jefferson John Quincy Adams Abraham Lincoln

12 12 Whole Line Output The program prints a line saying “done” if it finishes normally

13 13 Reading a Single Item To read multiple single items, you separate them from one another with commasTo read multiple single items, you separate them from one 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 oneItem, a string variableIn code samples we use oneItem, a string variable Input #1, oneItem Input #1, oneItem

14 14 Single Item Text File George, Washington John, Adams Thomas, Jefferson John, Quincy, Adams Abraham, Lincoln

15 15 Single Item Output

16 16 Read Multiple Items To do this, you provide more than one variable as a targetTo do this, you provide more than one variable as a target If your file does not have the right number of items, your program will terminate abnormallyIf your file does not have the right number of items, your program will terminate abnormally Here’s the code for reading two items at once: (ours are both strings but you can use other types if you know your data is numbers, for example)Here’s the code for reading two items at once: (ours are both strings but you can use other types if you know your data is numbers, for example) Input #1, item1, item2 Input #1, item1, item2

17 17 Two Item Text File George, Washington John, Adams Thomas, Jefferson Andrew, Jackson Abraham, Lincoln

18 18 Two Item Result

19 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

20 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 are some “invisible” characters in text files, such as carriage return and line feed.This may behave in an unexpected way since there are some “invisible” characters in text files, such as carriage return and line feed. Here’s the code; note the different syntax. Variable chars is of type String.Here’s the code; note the different syntax. Variable chars is of type String. chars = Input(N, #1) chars = Input(N, #1)

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

22 22 Reading one character at a time Lines 21 and 22 contain the carriage return and line feed characters

23 23 Two at a time: Now line 11 has the carriage return and line feed characters

24 24 Four at a Time Abnormal termination. The number of visible characters is 16, but the actual number is 18.

25 25 Why Read Characters? You might want to write code that picks out only certain kinds of items from the text, for example.You might want to write code that picks out only certain kinds of items from the text, for example. You can use string functions along with concatenation to build up things like a catalog of words in a text while ignoring punctuationYou can use string functions along with concatenation to build up things like a catalog of words in a text while ignoring punctuation Usually, though, reading one item at a time is the right approachUsually, though, reading one item at a time is the right approach


Download ppt "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."

Similar presentations


Ads by Google