Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3.5 Input and Output

Similar presentations


Presentation on theme: "Chapter 3.5 Input and Output"— Presentation transcript:

1 Chapter 3.5 Input and Output

2 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

3 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

4 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

5 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

6 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

7 Input

8 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

9 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

10 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

11 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

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

13 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

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

15 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

16 FILE IO OUTPUT

17 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

18 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

19 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

20 Formatting Report Output

21 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

22 Formatting Output with Format Functions
String Value FormatNumber( ,1) FormatNumber(1 + 2) 12,345.6 3.00 FormatCurrency( ,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

23 Code 12/30/2018 Input and Output

24 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( , 1)) lstDisplay.Items.Add(FormatNumber(1 + 2)) lstDisplay.Items.Add(FormatCurrency( ,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

25 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

26 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

27 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} {1,12:N0} 34.6 34 {1,12:C1} $1234.6 {1,-12:P} 0.569 56.90% 12/30/2018 Input and Output

28 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

29 Code 12/30/2018 Input and Output

30 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

31 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

32 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


Download ppt "Chapter 3.5 Input and Output"

Similar presentations


Ads by Google