Presentation is loading. Please wait.

Presentation is loading. Please wait.

INPUT AND OUTPUT.

Similar presentations


Presentation on theme: "INPUT AND OUTPUT."— Presentation transcript:

1 INPUT AND OUTPUT

2 OUTPUT TextBoxes & Labels MsgBox Function/Method MessageBox Class
Files (next topic) Printing (later)

3 To learn more about MsgBoxes, use Help.
MsgBox (text [,buttons [, caption/title]]) As MsgBoxResult MsgBox(“You must provide a file name”,,”File Error”) To learn more about MsgBoxes, use Help.

4 MsgBox MsgBox (text [,buttons [, caption/title]]) As MsgBoxResult
response = MsgBox(“Incorrect name-Continue?”, ,”File Error”) CType(4+32,MsgBoxStyle),”File Error”) When casting using CInt, we know the result will be an Integer. However, when using CType, the type is listed as the last parameter. Using numerical values is rather cryptic.

5 MsgBox response = MsgBox(“Incorrect name-Continue?”, CType(4+32,MsgBoxStyle),”File Error”) Instead of using the numerical values, we will use VB constants: response = MsgBox(“Incorrect name-Continue?", y, "File Error")) Ctype(MsgBoxStyle.YesNo+MsgBoxStyle.Question, MsgBoxStyle),"File Error") MsgBoxStyle.YesNo+MsgBoxStyle.Question, "File Error") MsgBoxStyle.YesNo+,"File Error") MsgBoxStyle.YesNo,"File Error") Now let’s look at the value for response.

6 MsgBox response = MsgBox(“Incorrect name-Continue?”, CType(MsgBoxStyle.YesNo+MsgBoxStyle.Question, MsgBoxStyle),"File Error") If(response = 7) Then ... vbNo) Then ... Now we have some code that is more understandable—Yes and No buttons, a Question mark icon, and we are checking for a negative response.

7 MessageBox MessageBox.Show (text [ ,caption/title [,buttons [, icon[ ,defaultbutton]]]]) MessageBox.Show("You must provide a file name", "File Error",MessageBoxButtons.OK) To learn more about MessageBoxes, use Help or see pages in the text.

8 INPUT Textboxes InputBox Function/Method Files (next topic)

9 To learn more about InputBoxes, use Help.
InputBox ( Prompt As String[, Title As String][, DefaultResponse As String][, XPos As Integer][, YPos As Integer]) As String fileName = InputBox("You must provide a file name", "File Error") If the user clicks OK, fileName will be grades. xlsx, and if the user clicks Cancel, fileName will be the empty string. To learn more about InputBoxes, use Help.

10 Accessing Files

11 Access Methods Sequential Access Method (SAM)
Direct (Random) Access Method (DAM) Indexed Sequential Access Method (ISAM)

12 Accessing Files We are going to limit our work with files and only use the Sequential Access Method (SAM) Text Files (which we will discuss) Binary Files (which we will not discuss)

13 Steps needed to transfer data to/from files
Include necessary libraries Declare a file stream object Open the file stream Transfer the data Close the file stream

14 Using the FileStream Class
SAM with Text Files Using the FileStream Class Before we can transfer data from a text file, we have to know how it is arranged. For our first example, the data are listed one measurement per line. When accessing a file, if no path is specified, then the file must be in the debug subfolder.

15 I/O is one of the areas in which c and c++ are different!
VB #include <fstream.h> ifstream inFile; inFile.open(“measures.txt”); inFile.close(); Imports System.IO Dim inFile As StreamReader inFile = File.OpenText (“measures.txt”) inFile.close() This will cause an error if the file does not exist. We will address trapping errors or catching exceptions in the next slide show. I/O is one of the areas in which c and c++ are different!

16 To add to an existing file, you would use File.AppendText
C++ VB #include <fstream.h> ofstream outFile; outFile.open(“results.txt”); outFile.close(); Imports System.IO Dim outFile As StreamWriter outFile = File.CreateText (“results.txt”) outFile.close() To add to an existing file, you would use File.AppendText

17 Let’s put the two previous slides together and create
a program the reads data from a file, performs a calculation, and writes the result to another file.

18 C++ VB Why are these needed?
#include <fstream.h> ifstream inFile; inFile.open(“measures.txt”); ofstream outFile; outFile.open(“results.txt”); . inFile.close(); outFile.close(); Imports System.IO Dim inFile As StreamReader Dim outFile As StreamWriter inFile = File.OpenText (“measures.txt”) outFile = File.CreateText (“results.txt”) . inFile.close() outFile.close() Why are these needed? Assume that we have: int length; Dim length As Integer int width; Dim width As Integer int area; Dim area As Integer inFile >> length; length = Cint(inFile.Readline) inFile >> width; width = Cint(inFile.ReadLine) area=length*width; area = length * width outFile << area << endl; outFile.WriteLine(area.ToString) NOT needed, the Write and WriteLine methods are “overloaded.” What other code must we add to this program?

19 Although we could use a For-Loop why is this inappropriate?
Imports System.IO . Dim inFile As StreamReader Dim outFile As StreamWriter inFile = File.OpenText (“measures.txt”) outFile = File.CreateText (“results.txt”) inFile.close() outFile.close() There are other ways to test for EOF, but this is the most readable, and we will use this approach! Assume that we have: Dim i As Integer For i = 1 To 3 Next i Do While (Not inFile.EndOfStream) Loop length = Cint(infile.Readline) width = Cint(infile.ReadLine) area = length * width outFile.WriteLine(area)

20 How Are Imported Text Files Delimited?

21 Although the previous example works, I think of the input file as:
Space delimited Tab delimited Comma delimited and the output file looking like: Unlike the text, I am using comma delimiters. Such files are referred to as CSV – Comma Separated Values. If we were using a data file containing name address city state zip, why would this be a poor choice for a delimiter? Let’s look at the code necessary to use a CSV file.

22 Before writing the code we need to examine a method and a function
The Split method is defined on page 285 and returns an array of strings, where each element contains a substring of the original string Split (split character) … name.Split (CChr(" ")) Strings are usually stored with a string count header and then the string. For example “Tony” is stored as 4 T o n y and “Z” is stored as 1 Z . The character Z is stored as just Z . Consequently, we must cast the string into a character. In a like manner, if we are using a space “ “, we must cast it into a character.

23 Anthony Joseph Nowakowski
Consider the following example: name first middle last Anthony Joseph Nowakowski Dim name, first, middle, last As String Dim fields(2) As String name = "Anthony Joseph Nowakowski“ Anthony fields = name.Split(CChar(" ")) first = fields(0) Joseph middle = fields(1) last = fields(2) Nowakowski fields Anthony Joseph Nowakowski

24 Before writing the code we need to examine a method and a function
The Split function is defined on page 295 and returns an array of strings, where each element contains a substring of the original string Split (string [,delimiterString] [,limit]) … Split(name) If the delimiter is omitted, then a space is used. If the limit is omitted then, all substrings are returned.

25 Before writing the code we need to examine a method and a function
Consider the following example: Dim name, first, middle, last As String Dim fields(2) As String name = "Anthony Joseph Nowakowski" fields = Split(name) first = fields(0) middle = fields(1) last = fields(2)

26 We now modify the code for the CSV example
Imports System.IO . Dim record, fields(1) As String Dim inFile As StreamReader Dim outFile As StreamWriter inFile = File.OpenText (“measures.csv”) outFile = File.CreateText (“results.csv”) Do While (Not inFile.EndOfStream) record = infile.ReadLine fields = Split(record,”,”) length = Cint(fields(0)) width = Cint(fields(1)) area = length * width outFile.Write(length & ”,”) outFile.Write(width & ”,”) outFile.WriteLine(area) Loop inFile.close() outFile.close() These create a new CSV file.

27 Our final text example involves the Gettysburg Address
Using the “traditional” format. i.e. paragraphs indented and no double-spacing between paragraphs. Add a vsb and read-only

28 Our final text example involves the Gettysburg Address
Text Box or Label? Imports System.IO . Dim inFile As StreamReader inFile = File.OpenText("GBA.txt") inFile.Close() lblGB.Text = inFile.ReadToEnd txtGB.Text = inFile.ReadToEnd Add a vsb and read-only

29 Some Other Considerations
What happens if we used the newer format? i.e. no indentation with double-spaced paragraphs. Add a vsb and read-only Instead of using ReadToEnd, what would happen if we used ReadLine to read each paragraph? The tabbed format is easier to implement.

30 Read the Description Of Assigment-4


Download ppt "INPUT AND OUTPUT."

Similar presentations


Ads by Google