Chapter 9 Files I/O: Files, Records and Fields Part 3.

14
Chapter 9 Files I/O: Files, Records and Fields Part 3

Transcript of Chapter 9 Files I/O: Files, Records and Fields Part 3.

Page 1: Chapter 9 Files I/O: Files, Records and Fields Part 3.

Chapter 9Files I/O:

Files, Records and Fields

Part 3

Page 2: Chapter 9 Files I/O: Files, Records and Fields 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.

Page 3: Chapter 9 Files I/O: Files, Records and Fields Part 3.

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

time.• 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 Olympia WA 5689263West 6• Oregon Salem OR 3281974West 6• North_Carolina Raleigh NC 7546493South 3

• OrWashington, Olympia, WA,5689263,West,6Oregon,Salem,OR,3281974,West,6North_Carolina,Raleigh,NC,7546493,South,3

or other formats

Page 4: Chapter 9 Files I/O: Files, Records and Fields Part 3.

Save File Dialog Box• Remember: All is ‘stream I/O’ and we create objects to help us use the

streams, as inDim swrFile As System.IO.Streamwriter (Know what this does!)

Recall also, we can1. Create a file from scratch (create) or2. Add to an existing sequential file (create and append)Via:

swrFile = System.IO.File.CreateText (‘filename.dat’)Or swrFile = System.IO.File.AppendText(‘filename.dat’)

And then we can write individual lines or individual items (tokens, attributes, fields, …) viaswrFile.WriteLine(strDetail) swrFile.Write(strDetail) Know the difference.

Lastly, we close, as expected:swrFile.Close()

What is Close()? AppendText? Where did they come from?

Page 5: Chapter 9 Files I/O: Files, Records and Fields Part 3.

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 that)

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.)

Page 6: Chapter 9 Files I/O: Files, Records and Fields Part 3.

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 2010\Projects\ParallelArrays\ParallelArrays\bin\Debug“ SaveAs.Title = "Save As ... " SaveAs.ShowDialog()

' 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 swrFile.WriteLine(str) ‘ Puts data into output stream

‘ who writes this to output stream swrFile.WriteLine(str1) swrFile.Close() ‘ Always Close()

End Sub

Page 7: Chapter 9 Files I/O: Files, Records and Fields Part 3.

Note the SaveAs Dialog Box title.Note the directory – defaultCan see my input file from previous slidesCan see the file output file I previously produced (if used again, it will ask if you want to replace existing version)

Insert filename or new file name

Page 8: Chapter 9 Files I/O: Files, Records and Fields Part 3.
Page 9: Chapter 9 Files I/O: Files, Records and Fields Part 3.

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.

Page 10: Chapter 9 Files I/O: Files, Records and Fields Part 3.

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!

Page 11: Chapter 9 Files I/O: Files, Records and Fields Part 3.

Study Sheet for Exam 3COP 2010 – Intro to Visual and Procedural Programming

Friday, 8 April 2010Closed book, closed notes, open minds, 50 minutes, in-class examEssentially Chapter 10 – Arrays, Structures, Sorting, and Searching

ArraysHow do declare

1d and 2d arraysUse of .length propertyArray indices – proper boundsProper array references Loading (populating) an array

Via loops from inputVia hard coding

Parallel arraysCreating 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)

StructureWhat is a structure?How does it differ from an array?Example?

Page 12: Chapter 9 Files I/O: Files, Records and Fields Part 3.

SortingBubbleSelectionInsertion

Characteristics and differences

Pseudo-code algorithms for each of these three types of sorts

Big O as it pertains to data structures / algorithmsWhat does O(n 2) mean? O(n)? O(log 2 n)?

Order of data after each pass for each type of sort

SearchingSequential searchBinary search

IterativeRecursive

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.

Page 13: Chapter 9 Files I/O: Files, Records and Fields Part 3.

Why / when would you use a binary search vice sequential search and why? Restrictions?

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 columnsumming elements in a diagonalTake 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)

Page 14: Chapter 9 Files I/O: Files, Records and Fields Part 3.

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!