Chapter 3.5 Input and Output

Slides:



Advertisements
Similar presentations
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Advertisements

Chapter 11 Data Files Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Input Dialog Box An input dialog box can be used to obtain a single item of input from the user Presents a window (dialog box) requesting input Syntax:
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
Chapter 9: Sequential Access Files and Printing
Input Validation Check the values entered into a text box before beginning any calculations Validation is a form of ‘self-protection’, rejecting bad data.
Chapter 8 - VB.Net by Schneider1 Chapter 8 – Sequential Files 8.1 Sequential Files Creating a Sequential File Adding Items to a Sequential File Error Trapping.
MIS316 – BUSINESS APPLICATION DEVELOPMENT – Chapter 14 – Files and Streams 1Microsoft Visual C# 2012, Fifth Edition.
Chapter 9 Files I/O: Files, Records and Fields. Basics of File Input and Output Have created both input and outputs from programs. Persistent data: What.
Chapter 3 - VB 2008 by Schneider1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
Input/Output CE 311 K - Introduction to Computer Methods Daene C. McKinney.
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
Using Arrays and File Handling
Chapter 3 - VB.NET by Schneider1 Chapter 3 – Fundamentals of Programming in VB.NET VB.NET Controls VB.NET Events Numbers Strings Input and Output.
Chapter 8 - VB 2008 by Schneider1 Chapter 8 – Sequential Files 8.1 Sequential Files 8.2 Using Sequential Files.
1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
Chapter 3 - VB 2008 by Schneider1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
Chapter 10: Structures and Sequential Access Files
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
Creating Sequential Files, Adding Data, & Deleting Data.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
11-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
Input Textboxes Input Boxes Different than textboxes Good for small amount of input (form full of textboxes is not nice) X = Inputbox(“prompt message”,
Reference: Lecturer Lecturer Reham O. Al-Abdul Jabba lectures for cap211 Files and Streams- I.
1 COMP3100e Developing Microsoft.Net Applications for Windows (Visual Basic.Net) Class 6 COMP3100E.
File I/O What We’ll Cover –Visual Basic Techniques for Text Files –.NET Techniques for Text Files What We’ll Not Cover –Binary File I/O –XML File I/O.
1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.
CS0004: Introduction to Programming Project 1 – Lessons Learned.
Tutorial 9: Sequential Access Files and Printing1 Tutorial 9 Sequential Access Files and Printing.
Numbers continued The Integer Data Type Multiple Declarations Parentheses Three Types of Errors.
Chapter 8 - Visual Basic Schneider
6-1 Chapter 6 Working with Arrays in VB.NET. 6-2 Learning Objectives Understand the use of list and table arrays in VB.NET projects and the difference.
Hungarian Notation A must in this course Every object used MUST be renamed including the form(s) using the following rules Form  frmFormName E.g. frmTemperature.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Ten Structures and Sequential Access Files.
BACS 287 File-Based Programming. BACS 287 Data Hierarchy  Database - Collection of files, relationships, integrity information, etc  Files - All records.
Compunet Corporation1 Programming with Visual Basic.NET Input and Output Files Lecture # 6 Tariq Ibn Aziz.
Introduction to VB programming Dr. John P. Abraham UTPA Chapters 2 & 3.
McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 11 Data Files.
Files and Streams. What is a file? Up until now, any stored data within a program is lost when the program closes. A file is a permanent way to store.
Files and Streams. Objectives Learn about the classes that support file input/output Understand the concept of abstraction and how it related to the file.
Microsoft Visual Basic 2012: Reloaded Fifth Edition Chapter One An Introduction to Visual Basic 2012.
Chapter 3 - VB 2008 by Schneider1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
1 Displaying Dialog Boxes Kashef Mughal. 2 Midterm Stats Here we go  Average was  Low was 116  High was 184  Mid Quarter Grade - check any.
Chapter 3 – Fundamentals of Programming in Visual Basic
Chapter 3 – Variables, Input, and Output
Reading & writing to files
Apply Procedures to Develop Message, Input, and Dialog Boxes
Files.
Files and Streams.
Chapter 8 – Sequential Files
Files and Streams Lect3 CT1411.
File Input/Output (I/O)
Fundamentals of Programming in VB.NET
String Variable, Methods and Properties
Section 3.3 Numbers Arithmetic Operations Variables
Variable Review & IO User 12/26/2018.
Input and Output.
String Variable, Methods and Properties
Tutorial 9 Sequential Access Files and Printing
Additional Topics in VB.NET
CIS16 Application Development and Programming using Visual Basic.net
String Variable, Methods and Properties
Introduction to Programming
String Variable, Methods and Properties
Input and Output.
Files and Streams.
Chapter 11 Saving Data and Objects In Files
Input and Output Chapter 3.5
Presentation transcript:

Chapter 3.5 Input and Output

Input and Output Input Output Reading Data from Files Getting Input from an Input Dialog Box Formatting Output with Format Functions Formatting Output with Zones Using a Message Dialog Box for Output Writing new Data to files 12/30/2018 Input and Output

Handling Files Two objects used to handle files Four step process StreamReader used to read files StreamWriter used to write files Four step process Declare an object variable to indicate object type Create the object Use object’s methods to perform operations Close the object 12/30/2018 Input and Output

Declaring Object Variables Two step process Declare variable name and type Dim PhoneFileI as System.IO.StreamReader System.IO referred to as namespace Create the object and associate file with variable PhoneFileI = New System.IO.StreamReader(filename) New keyword creates object and associates variable with file name Can be combined into one step Dim PhoneFileI as New System.IO.StreamReader(filename) 12/30/2018 Input and Output

Reading Data From a File StreamReader object used to read from a file StreamReader provides several methods Peek returns the next character, but does not advance Read reads specified number of characters and advances ReadLine reads one line of data starting from current position ReadToEnd reads remainder of file starting from current position Close dissociates object from physical file Closing the object after you have finished reading is crucial 12/30/2018 Input and Output

Testing for End of File Eventually, you get to the end of the file If you try to read more data, you get a runtime error Use Peek method to test for end of file Peek returns the next character in the file If there is no more data, Peek will return -1 Read more data if Peek method returns something other than -1 12/30/2018 Input and Output

Input

System.IO.StreamReader IO.StreamReader (INPUT) Reads data from sequential text files File mode for StreamReader is OpenText [System Keyword can be omitted] Declare StreamReader Object Dim sr as IO.StreamReader SR is the variable that you will use to reference the file in your code. Initialize the sr variable sr = IO.File.OpenText(filespec) 12/30/2018 Input and Output

File Specification filespec – the path to find the file. File specification consist of four parts Drive:\Folder\Name.Extension It should be assigned to a variable or enclosed in “” “C:\data\Payroll.txt” strFileName = “C:\data\Payroll.txt” If the file location is the bin folder of the application only the name.extension are necessary. Example: “Payroll.txt” 12/30/2018 Input and Output

Input data from a file Establish a communication link between computer and the disk drive for reading data from disk Dim readerVar As IO.StreamReader = _ IO.File.OpenText(filespec) DIM sr as IO.StreamReader = IO.File.OpenText(“Payroll.txt”) Read data in order, one at a time, from the file with the ReadLine method assuming the file contains one item of data per line. strVar = readerVar.ReadLine ‘readline method terminate the communications link readerVar.Close()’close method 1. Execute a statement of the form Dim readerVar As IO.StreamReader A StreamReader is an object from the Input/Output class that can read a stream of characters coming from a disk or coming over the Internet. The Dim statement declares the variable readerVar to be of type StreamReader. 2. Execute a statement of the form readerVar = IO.File.OpenText(filespec) where filespec identifies the file to be read. This statement establishes a communi-cations link between the computer and the disk drive for reading data from the disk. Data then can be input from the specified file and assigned to variables in the pro-gram. This assignment statement is said to “open the file for input.” Just as with other variables, the declaration and assignment statements in Steps 2 and 3 can be combined into the single statement Dim readerVar As IO.StreamReader = IO.File.OpenText(filespec) 3. Read items of data in order, one at a time, from the file with the ReadLine method. Each datum is retrieved as a string. A statement of the form strVar = readerVar.ReadLine causes the program to look in the file for the next unread line of data and assign it to the variable strVar. The data can be assigned to a numeric variable if it is first converted to a numeric type with a statement such as numVar = CDbl(readerVar.ReadLine) Note: If all the data in a file have been read by ReadLine statements and another item is requested by a ReadLine statement, the item retrieved will have the value Nothing. 4. After the desired items have been read from the file, terminate the communications link set in Step 3 with the statement readerVar.Close() 12/30/2018 Input and Output

Input Data From A File filespec – the path to find the file. A:\Grades.txt, C:\Grades.txt Default folder is bin folder Each datum (record) is retrieved as a String The item retrieved will have value Nothing if it is the end of the file (Peek) ‘Peek Method The data can be assigned to a numeric variable if converted into a numeric data type 12/30/2018 Input and Output

Code Our Program is designed to read three records from a sequential data file. 12/30/2018 Input and Output

Code ' Read data from a file and display in a list box Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click ' Establish a communication link Dim sr As IO.StreamReader = IO.File.OpenText("Grades.txt") ' Read data line by line and display them lstDisplay.Items.Clear() lstDisplay.Items.Add(sr.ReadLine()) ‘read line 1 lstDisplay.Items.Add(sr.ReadLine()) ‘read line 2 lstDisplay.Items.Add(sr.ReadLine()) ‘read line 3 ' Terminate the communication link sr.Close() End Sub 12/30/2018 Input and Output

Input Dialog Box strVar = InputBox(prompt, title) 12/30/2018 Input and Output

Code 12/30/2018 Input and Output ' Read data from a file ans display in a list box Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click Dim fileName As String Dim sr As IO.StreamReader ' Get the file name from user fileName = InputBox("Enter the file name", "Enter file name") ' establish a communication link sr = IO.File.OpenText(fileName) ' Read data line by line, display them and the average lstDisplay.Items.Clear() lstDisplay.Items.Add(sr.ReadLine()) ' Terminate the communication link sr.Close() End Sub 12/30/2018 Input and Output

FILE IO OUTPUT

System.IO.StreamWriter IO.StreamWriter (OUTPUT) Writes New data to a sequential file File modes for StreamWriter AppendText – adds records to end of file CreateText – creates a new instance of file 12/30/2018 Input and Output

StreamWriter Methods Write method writes the data to the file WriteLine method also writes data to file Adds an “end of line” marker at end Close method dissociates file from variable Closing the object after you have written all your data is crucial 12/30/2018 Input and Output

Code ' Write data to the end of a data file Private Sub btnWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWrite.Click ' Establish a communication link Dim sw As IO.StreamWriter = IO.File.AppendText("Grades.txt") Dim strGrade as string ' Write data line by line strGrade = txtGrade1.text sw.WriteLine(strGrade) ‘write line 1 strGrade = txtGrade2.text strGrade = txtGrade3.text ' Terminate the communication link sw.Close() End Sub 12/30/2018 Input and Output

Formatting Report Output

Format Functions FormatNumber(n,r) FormatCurrency(n,r) FormatPercent(n,r) return a string value n - a number, an numeric expression or a string to be formatted r - the number of decimal places default value is 2 12/30/2018 Input and Output

Formatting Output with Format Functions String Value FormatNumber(12345.628,1) FormatNumber(1 + 2) 12,345.6 3.00 FormatCurrency(12345.628,2) FormatCurrency(-100) $12,345.63 ($100.00) FormatPercent(0.185,2) FormatPercent(“0.07”) 18.50% 7.00% The Default # of Decimals is 2 12/30/2018 Input and Output

Code 12/30/2018 Input and Output

Code ' Format data using format functions Private Sub btnFormat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFormat.Click lstDisplay.Items.Add(FormatNumber(12345.628, 1)) lstDisplay.Items.Add(FormatNumber(1 + 2)) lstDisplay.Items.Add(FormatCurrency(12345.628,2)) lstDisplay.Items.Add(FormatCurrency(-100)) lstDisplay.Items.Add(FormatPercent(0.185,2)) lstDisplay.Items.Add(FormatPercent("0.07")) End Sub 12/30/2018 Input and Output

Formatting Output with Landing Zones Line up items in columns Use a fixed-width font Courier New Divide the characters into zones with a format string. 12/30/2018 Input and Output

Formatting output with zones Zone width Dim fmtStr As String = "{0, 15}{1, 10}{2, 8}“ lstOutput.Items.Add(String.Format(fmtStr, _ data0, data1, data2)) Zone number 12/30/2018 Input and Output

Formatting output with zones A colon and formatting symbol after width to specially format numeric data Zone Format Term Number to be formatted Number displayed {1,12:N3} 1234.5679 1234.568 {1,12:N0} 34.6 34 {1,12:C1} 1234.567 $1234.6 {1,-12:P} 0.569 56.90% 12/30/2018 Input and Output

Formatting Output with Zones Zone width left adjusted if preceded with minus sign, right adjusted otherwise Spaces between the successive pairs of brackets will be displayed in the corresponding zones in the output. 12/30/2018 Input and Output

Code 12/30/2018 Input and Output

Code 12/30/2018 Input and Output ' Read data from a file ans display in a list box Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click Dim fileName As String Dim sr As IO.StreamReader Dim fmtStr As String = "{0,-15}{1,15}{2,15}" ' Get the file name from user fileName = InputBox("Enter the file name", "Enter file name") ' Establish a communication link sr = IO.File.OpenText(fileName) ' Read data line by line, display them and the average lstDisplay.Items.Clear() lstDisplay.Items.Add(String.Format(fmtStr, "First Grade", "Second Grade", "Third Grade")) lstDisplay.Items.Add(String.Format(fmtStr, sr.ReadLine(), sr.ReadLine(), sr.ReadLine())) ' Terminate the communication link sr.Close() End Sub 12/30/2018 Input and Output

Using a Message Dialog Box for Output MsgBox(prompt,title) MsgBox(prompt, , title) is executed, where prompt and title are strings, a message dialog box appears with prompt displayed and the title bar caption title and stays on the screen until the user presses Enter, clicks on the box in the upper-right corner, or clicks OK. For instance, the state-ment MsgBox("Nice try, but no cigar.", , "Consolation") 12/30/2018 Input and Output

Code 'Message are sent before clearing the items in the list box Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click MsgBox(" All items will be deleted", , "Delete items") lstDisplay.Items.Clear() End Sub Primarily use to display Information or Error Messages 12/30/2018 Input and Output