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.

Slides:



Advertisements
Similar presentations
The INFILE Statement Reading files into SAS from an outside source: A Very Useful Tool!
Advertisements

Hand Crafting your own program By Eric Davis for CS103.
Excel 2007 ® Business and Personal Finances How can Excel 2007 help you format a workbook?
CS 206 Introduction to Computer Science II 09 / 09 / 2009 Instructor: Michael Eckmann.
1 CS 106, Winter 2009 Class 14, Section 4 Slides by: Dr. Cynthia A. Brown, Instructor section 4: Dr. Herbert G. Mayer,
1 CS 106, Winter 2009 Class 4, Section 4 Slides by: Dr. Cynthia A. Brown, Instructor section 4: Dr. Herbert G. Mayer,
By Mary Anne Poatsy, Keith Mulbery, Eric Cameron, Jason Davidson, Rebecca Lawson, Linda Lau, Jerri Williams Chapter 8 Get Connected 1 Copyright © 2014.
 Monday, 9/30/02, Slide #1 CS106 Introduction to CS1 Monday, 9/30/02  QUESTIONS (on HW02, etc.)??  Today: Libraries, program design  More on Functions!
Pasewark & Pasewark 1 Access Lesson 6 Integrating Access Microsoft Office 2007: Introductory.
Access Tutorial 8 Sharing, Integrating, and Analyzing Data
1 Access Lesson 6 Integrating Access Microsoft Office 2010 Introductory Pasewark & Pasewark.
Adding Controls to User Forms. Adding Controls A user form isn’t much use without some controls We’re going to add controls and write code for them Note.
Chapter 8 Printing 1. In COBOL you send data to the printer by writing data to a file. In COBOL, the printer is defined as a file, and it is opened, closed,
HTML and Designing Web Pages. u At its creation, the web was all about –Web pages were clumsily assembled –Web sites were accumulations of hyperlinked.
Excel 2007 Part (2) Dr. Susan Al Naqshbandi
1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material.
Microsoft Word 2000: Mail Merge Basics Peggy Serfazo Marple Molly Calvello Support Professionals Business Applications - Desktop Microsoft Corporation.
Spreadsheets and Microsoft Excel. Introduction n A spreadsheet (called a worksheet in Excel) is a two-dimensional array of cells containing data to be.
1 CS 106 Computing Fundamentals II Chapter 17 “Introduction To VBA” Herbert G. Mayer, PSU CS status 6/30/2013 Initial content copied verbatim from CS 106.
1 CS 106 Computing Fundamentals II Chapter 25 “Variables, Assignment Statement” Herbert G. Mayer, PSU CS Status 7/4/2013 Initial content copied verbatim.
1 CS 106 Computing Fundamentals II Chapter 29 “Creating A User Form” Herbert G. Mayer, PSU CS Status 7/4/2013 Initial content copied verbatim from CS 106.
1 CS 106 Computing Fundamentals II Chapter 7 “Showing Developer Tab” Herbert G. Mayer, PSU CS status 6/17/2013 Initial content copied verbatim from CS.
Miscellaneous Excel Combining Excel and Access. – Importing, exporting and linking Parsing and manipulating data. 1.
1 CS 106 Computing Fundamentals II Chapter 4 “Excel Basics for Mac” Herbert G. Mayer, PSU CS status 6/27/2013 Initial content copied verbatim from CS 106.
1 CS 106 Computing Fundamentals II Chapter 34 “Conditionals In Excel” Herbert G. Mayer, PSU CS Status 7/17/2013 Initial content copied verbatim from CS.
Copyright © 2008 Pearson Prentice Hall. All rights reserved Copyright © 2008 Prentice-Hall. All rights reserved. Committed to Shaping the Next.
INSERT BOOK COVER 1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Excel 2010 by Robert Grauer, Keith.
McGraw-Hill/Irwin The Interactive Computing Series © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft Excel 2002 Lesson 1 Introduction.
WORKBOOK FORMATTING Nolan Tomboulian Tomboulian.wikispaces.com HOW THINGS LOOK CELL COLORFONT COLOR CELL BORDERSFONT SIZE CELL SIZEFONT.
1 CS 106 Computing Fundamentals II Chapter 61 “Loops” Herbert G. Mayer, PSU CS Status 7/29/2013 Initial content copied verbatim from CS 106 material developed.
Working With Files. 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.
1 CS 106 Computing Fundamentals II Chapter 81 “Error Handling” Herbert G. Mayer, PSU CS status 6/14/2013 Initial content copied verbatim from CS 106 material.
1 CS 106 Computing Fundamentals II Chapter 84 “Array Formulae” Herbert G. Mayer, PSU CS status 6/14/2013 Initial content copied verbatim from CS 106 material.
CCS – Mail Merge Mail Merge This presentation is incomplete without the associated discussion 1 Coloma Community Schools In-service 21 March 2014.
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.
1 CS 106 Computing Fundamentals II Chapter 23 “Controls And Events” Herbert G. Mayer, PSU CS Status 7/5/2013 Initial content copied verbatim from CS 106.
Files Tutor: You will need ….
Importing Data to Excel. Suppose you have a delimited* text file and you need to bring it into Excel. Follow these steps… *Delimited means text separated.
1 CS 106 Computing Fundamentals II Chapter 211 “Coding And Debugging” Herbert G. Mayer, PSU CS status 6/29/2013 Initial content copied verbatim from CS.
1 CS 106 Computing Fundamentals II Chapter 210 “Adding Controls to User Forms” Herbert G. Mayer, PSU CS Status 7/4/2013 Initial content copied verbatim.
Unit 3: Text, Fields & Tables DT2510: Advanced CAD Methods.
Mail merge I: Use mail merge for mass mailings Overview: Mailings en masse What if you need to send to each of your employees a letter containing personal.
1 CS 106 Computing Fundamentals II Chapter 6 “How To Zip & Unzip Files” Herbert G. Mayer, PSU CS status 6/17/2013 Initial content copied verbatim from.
1 CSE 2337 Chapter 7 Organizing Data. 2 Overview Import unstructured data Concatenation Parse Create Excel Lists.
JavaScript Introduction and Background. 2 Web languages Three formal languages HTML JavaScript CSS Three different tasks Document description Client-side.
1 CS 106 Computing Fundamentals II Chapter 28 “Scope” Herbert G. Mayer, PSU CS Status 7/14/2013 Initial content copied verbatim from CS 106 material developed.
Copyright © Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs.
Java I/O Basics MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh Bajaj,
Exporting & Formatting Budgets from FlexGen, NextGen & Zortec into Excel.
1 CS 106 Computing Fundamentals II Chapter 42 “Sub Procedures And Functions” Herbert G. Mayer, PSU CS Status 8/5/2013 Initial content copied verbatim from.
Copyright 2007, Paradigm Publishing Inc. BACKNEXTEND 8-1 LINKS TO OBJECTIVES Import data from another Access table Import data from another Access table.
Copyright 2007, Paradigm Publishing Inc. EXCEL 2007 Chapter 8 BACKNEXTEND 8-1 LINKS TO OBJECTIVES Import data from Access, a Web site, or a CSV text file.
1 CS 106 Computing Fundamentals II Chapter 85 “Excel Tables” Herbert G. Mayer, PSU CS status 6/14/2013 Initial content copied verbatim from CS 106 material.
CS 106 Computing Fundamentals II Chapter 5 “Excel Basics for Windows”
Step 1: Prepare data in Excel for mail merge
Prepare data for importing
CS 106 Computing Fundamentals II Chapter 35 “Controls For Choices”
Getting Started with C.
Exploring Microsoft® Access® 2016 Series Editor Mary Anne Poatsy
CS 106 Computing Fundamentals II Chapter 71 “Indexing”
CS 106 Computing Fundamentals II Chapter 73 “Ranges”
Access Tutorial 8 Sharing, Integrating, and Analyzing Data
CS 106 Computing Fundamentals II Chapter 66 “Working With Strings”
Microsoft Excel 2007 – Level 2
Herbert G. Mayer, PSU CS Status 8/2/2013
CS 106 Computing Fundamentals II Chapter 64 “For Loop Variations”
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
CS 106 Computing Fundamentals II Chapter 69 “Event Loop”
Tutorial 8 Sharing, Integrating, and Analyzing Data
Presentation transcript:

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 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 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 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 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 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 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 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 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 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 Whole Line Text File George Washington John Adams Thomas Jefferson John Quincy Adams Abraham Lincoln

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

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 Single Item Text File George, Washington John, Adams Thomas, Jefferson John, Quincy, Adams Abraham, Lincoln

15 Single Item Output

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 Two Item Text File George, Washington John, Adams Thomas, Jefferson Andrew, Jackson Abraham, Lincoln

18 Two Item Result

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 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 The Data File This file ends with a return, which you can’t see. But look at the results…

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

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

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

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