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

Chapter 9 Files I/O: Files, Records and Fields. Basics of File Input and Output Have created both input and outputs from programs. Persistent data: What

Embed Size (px)

Citation preview

Chapter 9Files I/O:

Files, Records and Fields

Basics of File Input and Output• Have created both input and outputs from programs.• Persistent data: What is it?• Temporary data• Program terminated data is gone.

• Have need to store and retrieve data from permanent stores.• Files:• Sequential• Random

• Text• Binary

Basic Data Processing• Basic data processing: • Open a data file• Read data• Process the data• Display outputs• Write results to a file• Close the file

• Files can contain millions of records; frequently hundreds of thousands of records.

• E.g. LPS downtown (home loans)

The Basics of File Input and Output

• Our files: text files – character data• Each line = record• Record consists of fields• E.g. Student record:• One per student• Fields: Name, n-number, address, major, gpa, phone number, …

• File: Sum of all student records = a file• All have same format, same length• (not always true…)

• Access: Sequential access: access one record at a time in order.

The Basics of File Input and OutputBatch Processing

• Batch Processing: “a technique for processing a set of data records as a group; each record is read and processed individually and in sequential order”

• Pseudo-code for batch file processing is typically:• Display a header What is a header?• Open input file• Do

• Read a record• Process the record• Post results

• Until all records are read• Close file

The Basics of File Input and OutputBatch Processing - more

• Pre-process:• Display a header ‘What is a header?• Open input file(s)• Read first record of file ‘ Called ‘priming the pump.’

• Process• Do while not EOF ‘ EOF?

• Process the record• Post results (print, display, write to another file…)

• Read next record in file• Loop

• Post Process• Generate statistics, summary information, etc if required• Write trailer, if printing a report• Close File(s)

Sequential File Access - 1• Often talk in terms of ‘streams’• The stream of text to or from a data file• We control the stream of data coming in from a file or being written

to a file, but we do this with help from ‘objects’ that supply ‘methods’ to us to facilitate ‘reading’ and ‘writing’ data to a stream.

• Consider: Dim srdFile as System.IO.StreamReader• Declares / names an object called srdFile from StreamReader class.• srd stands for stream reader.

• Consider: srdFile = New System.IO.StreamReader (“GPA.dat”)• Actually creates the object and associates it with the file, GPA.dat.

• Also ‘opens the file.’• File prepared ahead of time must be in bin\Debug folder of your

project.• Must be in this subdirectory or program will crash.

Sequential File Access - 2• Much better ways to do this.• Immediate downside:• File name is hardcoded in program. So what??• No real flexibility here

• Declare and create object at same time:• Dim srdFile as New System.IO.StreadReader (“GPA.dat”)

• Why create objects?• For classes defined in the language, you get the methods!!

• Example: strLine = srdFile.ReadLine() • does what? Returns what?• Invokes ReadLine() method in the object srdFile, which reads the next line in the

file, and returns a string, which is assigned to strLIne for subsequent processing.

• Let’s put this stuff together…

Sequential File Access – 3Code Example (book)

Private Sub btnRead_Click… Dim srdFile as System.IO.StreamReader ‘ what does this do? Dim strLine as String rtbOut.Clear() ‘ use lbc.Item.Add(…..) as you wish… rtbOut.AppendText (“ Student GPA” & ControlChars.NewLine &_

ControlChars.NewLine) ‘ what does this do? Why? srdFile = New System.IO.StreamReader (“GPA.dat”) ‘what does this do? Do until srdFile.Peek = -1 ‘ what is Peek?? method?

strLine = srdFile.ReadLine()rtbOut.AppendText (strLine & vbNewLine)

Loop srdFile.Close()End Sub‘ no preprocessing shown; no real processing shown; no post processing shown. Think of any examples?

More Significant Programming Example (book)

• Files are organized in different ways• Fixed length fields (occupying specified position)• Comma-delimited fields• Space delimited fields• Others (can specify)

• Typically, we read in a record at a time via readLine() method.• Typically, record formats are fixed in number of fields and data

types of fields (six fields: string, string, four ints) • But we must ‘parse’ the record into its constituent fields.• So, how to do this? Pretty easy, IF the record is fixed.

More Significant Programming Example (book)

• Dim strRecord() as String ‘ note: array of Strings.

• strLine = srdFinanceCharges.ReadLine • ‘srdFinanceCharges is object associated with the file name• ‘ reads a line from srdFinanceCharges object via ReadLine

• strRecord = strLine.Split(“,”) ‘ parse line using comma delimiter• strLast = strRecord(0)• strFirst = strRecord(1)• strAcctNo = strRecord(2)• decBalance = Convert.ToDecimal (strRecord(3))• tryParse(strRecord(3), decBalance)

• Given input record: Anderson, Andy, 39495, 2127.43• We’d get:• strRecord(0) = “Anderson”• strRecord(1) = “Andy”• strRecord (2) = “39495”• strRecord(3) = “2127.43”

More Significant Programming Example (book)

• Note ALL are strings coming in from the input file. • The first three fields are strings and they remain as strings.• The code converts in the fourth case: decBalance = Convert.ToDecimal (strRecord(3))

(tryParse(strRecord(3), decBalance))

Process: We can now process as desired….Book reads each record and processes it,

Multiplies decBalance by a percentage to determine finance charge.

Accumulates total of the finance charges for all records.Produces a line of output in a rich Text Box.

Let’s look at this code before looking at Postprocessing:

More Significant Programming Example (book)

• rtbOutput.Appendtext (strLast.PadRight(12) &_ strFirst.PadRight(12) & strAcctNo.PadRight(10) &_

decBalance.ToString (“c”).PadLeft(11) &_ decFinanceCharge.ToString (“c”.PadLeft(10) & vbNewLine)

PadLeft() merely adds spaces to the left of the string, while PadRight() adds spaces to the right of the string.

This also defines field size and brings about very nice alignment of columnar data.

See figure 9.3 in text.

More Significant Programming Example (book)

• Postprocessing:• Close the file and produce any summary data.• This is usually totals, or things like that.

• From the program:• srdFinanceCharges.Close()

• rtbOut.AppendText (“Total Charges: “ &_ decTotal.ToString(“c”).PadLeft(41) & vbNewLine)

File Output - 1• Can create a file offline using a word processor or a

spreadsheet or• Build the file from a program.

• Pretty straightforward.• Two methods available to objects in StreamWriter• CreateText (“file name”) and AppendText (“file name”)• CreateText(“filename”) creates a file; replaces existing file, so

be careful! Discuss.• AppendText (“filename”) creates a file, if it does not exist or

appends the records to the end of the file, if the named file does exist.

File Output - 2• Consider the code:• Dim swrFile As System.IO.StreamWriter• swrFile = System.IO.File,CreateText(“filename.dat”)

• Or• swrFile = System.IO.File.AppendText(“filename.dat”)• swrFile.WriteLine(strDetail) ‘ assumes comma-delimited fields• swrFile.Close()

• Alternatively, if you want to build an output line one field at a time, you might write:

swrFile.Write(strDetail), which will write a field to a record.To end the record, your last line ought to be a WriteLine() or a Write() followed by a vbNewLine to terminate the detail line.

Dialog Boxes• Interested in Save File dialog box and Open File Dialog boxes.• Toolbox contains a whole set of these dialog boxes (bottom)• Add them to the component tray at the bottom of your forms.

• Allows us to specify an input file or output file name at run time! (like “testFile.dat”)

• Additional features: Can specify a directory and full path as well, such as C:\myDirectory\COP2010\wgabra.txt

• Consider:

Open File Dialog Boxes• OpenFileDialog Control• Once OpenFileDialog box is in your component tray, we activate with the code: ofdOpenFile.ShowDialog that

allows the user to browse through drives and folders to determine the file to be opened. (Press OK or Cancel)

• Selected filename is copied into the FileName property of the OpenFileDialog control and we now have the srdFile object pointing to the desired ofdOpenfile.Filename.

• Code might appear as:• srdFile = New System.IO.StreamREader(ofdOpenFile.Filename)• This allows the user to determine at run time the file to be used in his/her

program.• Can also set up a Filter to display only files with .dat extension (*.dat) or

any files (*.*)…See book for more details.

Save File Dialog Boxes• Used for saving files produced under program control. • Just as for the OpenFileDialog box, the SaveFileDialog box is added to the

component tray and is activated the same way: sfdSaveFile.ShowDialog() which results in displaying the Save File Dialog box.

• With a Save As dialog box, user enters name or fully-qualified name and drive and folder to designate where the file gets saved.

• There is a Save button and a Cancel button just as there is an OK button and a Cancel button for the OpenFileDialog Box.

• Example: (book)If sfdSaveFile.ShowDialog = Windows.Forms.DialogResult.OK Then

swrFile = System.IO.File.AppendText(sfdSaveFile.FileName)‘ write records to the fileswrFle.Close()

ElseMessageBox.Show (“Operation cancelled.”)

EndIf

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.