Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "INPUT AND OUTPUT 1. OUTPUT TextBoxes & Labels MsgBox Function/Method MessageBox Class Files (next topic) Printing (later) 2."— Presentation transcript:

1 INPUT AND OUTPUT 1

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

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

4 response = MsgBox(“Incorrect name-Continue?”, 4+32,”File Error”) MsgBox 4 CType(4+32,MsgBoxStyle),”File Error”) MsgBox (text [,buttons [, caption/title]]) As MsgBoxResult When casting using CInt, we know the result will be an Integer. However, when using CType, the type is listed as the last parameter. 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 response = MsgBox(“Incorrect name-Continue?", y, "File Error")) MsgBox Instead of using the numerical values, we will use VB constants: 5 MsgBoxStyle.YesNo,"File Error") response = MsgBox(“Incorrect name-Continue?”, CType(4+32,MsgBoxStyle),”File Error”) MsgBoxStyle.YesNo+,"File Error") MsgBoxStyle.YesNo+MsgBoxStyle.Question, "File Error") Ctype(MsgBoxStyle.YesNo+MsgBoxStyle.Question, MsgBoxStyle),"File Error") Now let’s look at the value for response.

6 MsgBox 6 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 320-321 in the text.Help 7

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

9 InputBox 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") To learn more about InputBoxes, use Help.Help 9 If the user clicks OK, fileName will be grades. xlsx, and if the user clicks Cancel, fileName will be the empty string.

10 Accessing Files 10

11 Access Methods 1.Sequential Access Method (SAM) 2.Direct (Random) Access Method (DAM) 3.Indexed Sequential Access Method (ISAM) 11

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

13 1.Include necessary libraries 2.Declare a file stream object 3.Open the file stream 4.Transfer the data 5.Close the file stream 13 Steps needed to transfer data to/from files

14 14 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. SAM with Text Files Using the FileStream Class When accessing a file, if no path is specified, then the file must be in the debug subfolder.

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

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

17 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++ #include ifstream inFile; inFile.open(“measures.txt”); ofstream outFile; outFile.open(“results.txt”);. inFile.close(); outFile.close(); VB 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() 18 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) Assume that we have: int length;Dim length As Integer int width;Dim width As Integer int area;Dim area As Integer Assume that we have: int length;Dim length As Integer int width;Dim width As Integer int area;Dim area As Integer Why are these needed? NOT needed, the Write and WriteLine methods are “overloaded.” What other code must we add to this program?

19 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() length = Cint(infile.Readline) width = Cint(infile.ReadLine) area = length * width outFile.WriteLine(area) Do While (inFile.Peek <> -1) Loop Assume that we have: Dim i As Integer Assume that we have: Dim i As Integer For i = 1 To 3 Next i

20 20 How Are Imported Text Files Delimited?

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

22 The Split method is defined on page 281and returns an array of strings, where each element contains a substring of the original string Split (split character) … name.Split (CChr(" ")) 22 Before writing the code we need to examine a method and a function 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. 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 23 Consider the following example: Dim name, first, middle, last As String fields = name.Split(CChar(" ")) name first middle last Anthony Joseph Nowakowski name = "Anthony Joseph Nowakowski“ Dim fields(2) As String fields Anthony Joseph Nowakowski first = fields(0) Anthony Joseph Nowakowski middle = fields(1) last = fields(2)

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

25 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 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 (inFile.Peek <> -1) 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 Using the “traditional” format. i.e. paragraphs indented and no double- spacing between paragraphs. 27 Our final text example involves the Gettysburg Address

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

29 29 Some Other Considerations What happens if we used the newer format? i.e. no indentation with double-spaced paragraphs. 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 30


Download ppt "INPUT AND OUTPUT 1. OUTPUT TextBoxes & Labels MsgBox Function/Method MessageBox Class Files (next topic) Printing (later) 2."

Similar presentations


Ads by Google