Chapter 9
Files I/O:
Files, Records and
Part 3
Last Materials on Files for Now
• There are many important topics on Files we will not cover
• Reports
Detail data
Summary data / reports
Exception reports
Voluminous outputs
• Control breaks – know the theory behind this. (discuss)
• Files
Master files
Transaction files
Batch updates / interactive updates
Temporary files
• Error lists
• Change lists
• Finish up with a bit more on dialog boxes and move to databases.
Save File Dialog Box
• SaveFileDialog Box is similar to the OpenFile Dialog Box.
• Allows user to Save a file without specifying a path at compile
• Initially from scratch
• Modified the contents of a file.
• Rename as well.
• We often want to write data to an output
• Remember: Data must be written to files before it can be
opened and processed, regardless of the format of the data.
• Washington
• Oregon
• North_Carolina
• Or
Washington, Olympia, WA,5689263,West,6
or other formats
WA 5689263West
OR 3281974West
NC 7546493South
Save File Dialog Box
• Remember: All is ‘stream I/O’ and we create objects to help us use the
streams, as in
Dim swrFile As System.IO.Streamwriter
Recall also, we can
(Know what this does!)
1. Create a file from scratch (create) or
2. Add to an existing sequential file (create and append)
swrFile = System.IO.File.CreateText (‘filename.dat’)
swrFile = System.IO.File.AppendText(‘filename.dat’)
And then we can write individual lines or individual items (tokens,
attributes, fields, …) via
Know the difference.
Lastly, we close, as expected:
What is Close()? AppendText? Where did they come from?
Save File Dialog Box
• But we want to use the Save File Dialog Box….
• Used for saving files produced under program control.
1 Double click on the SaveFileDialog control in your toolbox, and a
SaveFileDialog box will appear in your component tray.
2. Go to the properties box and change the name to SaveAs (or something like
3. Go to your menu: File, SaveAs… and click it. This will bring you to your
code window.
(You can save a file anywhere, and you can set an initial directory and a
filter if you wish as in OpenFileDialog.) You will get your event handler:
Private Sub SaveAsToolStripMenuItem_Click
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles SaveAsToolStripMenuItem.Click
(This is the code I used; You may modify as you wish, but this worked.)
Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles SaveAsToolStripMenuItem.Click
Dim str As String
Dim str1 As String
str = "That is All Folks!"
str1 = "War Eagle!“
‘ National Champions 2010
' Housekeeping Declarations
Dim swrFile As System.IO.StreamWriter
‘ declares swrFile as variable
SaveAs.InitialDirectory = "C:\Users\Bob\My Documents\Visual Studio
SaveAs.Title = "Save As ... "
' Insert desired file name to Save As...
' Inputted name is assigned to FileName parameter
' Creates an object swrFile that points to the file in FileName
swrFile = System.IO.File.AppendText(SaveAs.FileName) ‘ declares swrFile object
' Prepares File for outline data
‘ Puts data into output stream
‘ who writes this to output stream
‘ Always Close()
End Sub
Insert filename or ne
Note the SaveAs Dialog Box title.
Note the directory – default
Can see my input file from previous slides
Can see the file output file I previously produced (if used again, it will ask if you want to replace
Input / Output
• In all languages, I/O is a set of activities that likely cause more
problems than any other set of instructions.
• Very error prone.
• In files, all records must have the same structure
• Order of fields not important, but the order must be same
• Be careful about the delimiter or field size
• File cannot have a blank line in it or miss data.
• File paths must be accurate.
• If you open a file, close it.
• Cannot backup in a sequential file; cannot skip around.
• Understand Create a file and Append to a file.
• Use Try…Catch blocks especially for I/O.
Name Spaces
• Similar to the Java API
• Contains all the classes and methods and descriptions
available to you in VB.
• Depress F2 to bring up the NameSpace.
• Everything is hierarchical.
• E.g. System.IO namespace controls the IO functions.
• Press F2
• See Microsoft VisualBaseic.FileIO
• See TextFieldParser
• Loop up SystemIO You get objects and method descriptions!
Study Sheet for Exam 3
COP 2010 – Intro to Visual and Procedural Programming
Friday, 8 April 2010
Closed book, closed notes, open minds, 50 minutes, in-class exam
Essentially Chapter 10 – Arrays, Structures, Sorting, and Searching
How do declare
1d and 2d arrays
Use of .length property
Array indices – proper bounds
Proper array references
Loading (populating) an array
Via loops from input
Via hard coding
Parallel arrays
Creating one array of different size from another
e.g. array (m,n) to vector (m) (done in class)
Passing arrays to functions (always by address, even if specify ByVal)
What is a structure?
How does it differ from an array?
Characteristics and differences
Pseudo-code algorithms for each of these three types of sorts
Big O as it pertains to data structures / algorithms
What does O(n 2) mean? O(n)? O(log 2 n)?
Order of data after each pass for each type of sort
Sequential search
Binary search
Best, average, and worst cases;
Scenarios and number of operations
Searching through, say, only a column or only a specific row of a 2d array.
Searching through a populated 2d array counting the occurrences of, say, grades over
70 and
less than 80; 80 and less than 90….. Display counts at end.
Why / when would you use a binary search vice sequential search and why?
Be certain to study the chapter terms, concepts, and questions at the end of
chapter for some ‘free’ objective questions. They add up!
Will give three or four performance questions and allow you to select,
perhaps, two of these, likely from the suggestions above.
manipulating array elements, such as summing items in a column
summing elements in a diagonal
Take A(m,n) and make it A(n,m) (Transpose of an array)
Create a column vector from each row of a 2d array (done in class)
Exam 4 will be on 22 April
• Coverage:
• Files
• Databases
• There will not be a comprehensive final exam.
• Justin’s grading system will be used as much as possible. I do
not anticipate any variation from her grading scheme at this
point in time.
• Project 4 – TBD. Will be on Databases.
• Will want you to create, update, and query your database.
• Small program, in the interest of time, but you need this for Web
Access course!

Chapter 9 Files I/O: Files, Records and Fields