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

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 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 Output Batch 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 Open input file Do What is a header?

Read a record Process the record Post results Until all records are read Close file The Basics of File Input and Output Batch 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.

No real flexibility here So what?? 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. Lets put this stuff together Sequential File Access 3

Code 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 Wed 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.

Lets 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 file swrFle.Close() Else MessageBox.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 TryCatch 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.

Recently Viewed Presentations

  • Review for Midterm 1 -

    Review for Midterm 1 -

    A skier covers a distance of 3 m in half a second. What is his average speed? A) 1.5 m/s. B) 3 m/s. C) 6 m/s. D) 9 m/s. ... As indicated. the center of gravity of Truck 1 is...
  • Quiz Bio161 Q1. The theory that holds that

    Quiz Bio161 Q1. The theory that holds that

    You have compared the phenotypic ratio of the offspring to that predicted by a dihybrid cross.You calculated P>0.05. P is the probability. With which the dihybrid model by chance alone correctly explains the cross. That you are correct by chance...
  • Scientific Method - Science with Mr. Jones

    Scientific Method - Science with Mr. Jones

    Constants Factors in an experiment (both in the experimental and control groups) that are kept the same and not allowed to change Examples One minute was consistently the amount of time allotted to perform the necessary activity The type of...
  • NC Concealed Carry Handgun PowerPoint Instructional Presentation Compendium

    NC Concealed Carry Handgun PowerPoint Instructional Presentation Compendium

    This presentation is provided as information to be shared with NC CCA and their members. This presentation may not copied, downloaded or an any manner transmitted or conveyed to any third party without prior written consent by personal Defense Training...
  • OSLER JOURNAL CLUB COHORT STUDY 8/12/09 Racial Differences

    OSLER JOURNAL CLUB COHORT STUDY 8/12/09 Racial Differences

    Recall bias. Cannot address prevalence or incidence. Current Article. Bibbins-Domingo et al. NEJM 2009; 360:1179-90. Study question: Association of ethnicity with heart failure in young adults. Results: Young African Americans have greater risk of heart failure than young Americans of...
  • CCT - Microsoft Azure

    CCT - Microsoft Azure

    CCT application. Earliest 6 months prior to CCT. Cannot start Consultant post without CCT. Can work in locum Consultant post for 1 year maximum without CCT. Acting up can count towards training (3-6months) but must be planned carefully. Consultant interview...
  • Revolts in the Empire - Oak Park Independent

    Revolts in the Empire - Oak Park Independent

    British East India Company gained power in India after the decline of Mughal Empire in the early 1600's [spread from Bengal] controlled 3/5 of India by the mid-1900's. Exploited Indian disunity in culture and language + guns to conquer. ......
  • Year 7 Design and Technology 1. Tools and

    Year 7 Design and Technology 1. Tools and

    1. Tools and equipment. Vice. Bench hook. Coping saw. Tenon saw. Marking gauge. Try square. Year 7 Design and Technology. Glue gun. Drill bit. Safety glasses. File