INPUTAND
OUTPUT1
OUTPUTTextBoxes & Labels
MsgBox Function/Method
MessageBox Class
Files (next topic)
Printing (later)
2
MsgBoxMsgBox (text [,buttons [, caption/title]]) As MsgBoxResult
MsgBox(“You must provide a file name”,,”File Error”)
3
To learn more about MsgBoxes, use Help.
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.Using numerical values is rather cryptic.
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.Now let’s look at the value for response.
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 moreunderstandable—Yes and No buttons,a Question mark icon, and we arechecking for a negative response.
Now we have some code that is moreunderstandable—Yes and No buttons,a Question mark icon, and we arechecking for a negative response.
MessageBoxMessageBox.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.
7
INPUTTextboxes
InputBox Function/Method
Files (next topic)
8
InputBoxInputBox ( 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.
9
If the user clicks OK, fileName will be grades. xlsx, andif the user clicks Cancel, fileName will be the empty string.
If the user clicks OK, fileName will be grades. xlsx, andif the user clicks Cancel, fileName will be the empty string.
AccessingFiles
10
Access Methods
1. Sequential Access Method (SAM)
2. Direct (Random) Access Method (DAM)
3. Indexed Sequential Access Method (ISAM)
11
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
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 transferdata to/from files
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 FilesUsing the FileStream Class
When accessing a file, if no path is specified,then the file must be in the debug subfolder.
When accessing a file, if no path is specified,then the file must be in the debug subfolder.
C++
#include <fstream.h>ifstream inFile;inFile.open(“measures.txt”);
.
.
.
inFile.close();
VB
Imports System.IODim inFile As StreamReaderinFile = File.OpenText (“measures.txt”)
.
.
.
inFile.close()
15
I/O is one of the areas inwhich c and c++
are different!
I/O is one of the areas inwhich c and c++
are different!
This will cause an error if the file does notexist. We will address trapping errors or
catching exceptions in the next slide show.
This will cause an error if the file does notexist. We will address trapping errors or
catching exceptions in the next slide show.
C++
#include <fstream.h>ofstream outFile;outFile.open(“results.txt”);
.
.
.
outFile.close();
VB
Imports System.IODim outFile As StreamWriteroutFile = File.CreateText (“results.txt”)
.
.
.
outFile.close()
16
To add to an existing file, you would use
File.AppendTextTo add to an existing file, you would use
File.AppendText
17
Let’s put the two previousslides together and createa program the reads data
from a file, performs acalculation, and writes the
result to another file.
C++
#include <fstream.h>ifstream inFile;inFile.open(“measures.txt”);ofstream outFile;outFile.open(“results.txt”);
...
...
inFile.close();
outFile.close();
VB
Imports System.IODim inFile As StreamReaderDim outFile As StreamWriterinFile = 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 * widthoutFile << 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?Why are these needed?
NOT needed, the Write and WriteLine methods are “overloaded.”
NOT needed, the Write and WriteLine methods are “overloaded.”
What other code must weadd to this program?
What other code must weadd to this program?
19
Although we could use a For-Loopwhy is this inappropriate?
Imports System.IO
...
Dim inFile As StreamReaderDim outFile As StreamWriterinFile = 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
How Are Imported Text Files Delimited?
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.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. Suchfiles are referred to as CSV – Comma Separated Values.
Unlike the text, I am using comma delimiters. Suchfiles 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?
• 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 thestring. 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 mustcast 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 thestring. 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 mustcast the string into a character. In a like manner, if we are using a space “ “, we must cast it into a character.
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)
• 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
Before writing the code we need to examine a method and a function
Consider the following example:
Dim name, first, middle, last As StringDim fields(2) As Stringname = "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 StringDim inFile As StreamReaderDim outFile As StreamWriterinFile = 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.These create a new CSV file.
Using the “traditional” format. i.e. paragraphs indented and no double-
spacing between paragraphs.
27
Our final text example involvesthe Gettysburg Address
28
Our final text example involvesthe Gettysburg Address
Imports System.IO
...
Dim inFile As StreamReaderinFile = File.OpenText("GBA.txt") inFile.Close()txtGB.Text = inFile.ReadToEnd
Text Box or Label?Text Box or Label?
lblGB.Text = inFile.ReadToEnd
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.
Read the
Description
Of Assigment-4
30