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.

Slides:



Advertisements
Similar presentations
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Ticket Information Application.
Advertisements

File Handling Advanced Higher Programming. What is a file? Up until now, any stored data within a program is lost when the program closes. A file is a.
Alford Academy Business Education and Computing1 Advanced Higher Computing Based on Heriot-Watt University Scholar Materials File Handling.
C# - Files and Streams Outline Files and Streams Classes File and Directory Creating a Sequential-Access File Reading Data from a Sequential-Access.
Chapter 11 Data Files Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Files & Streams. Files Introduction Files are used for long-term retention of large amounts of data, even after the program that created the data terminates.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
Chapter 9: Sequential Access Files and Printing
Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.
MIS316 – BUSINESS APPLICATION DEVELOPMENT – Chapter 14 – Files and Streams 1Microsoft Visual C# 2012, Fifth Edition.
Input/Output CE 311 K - Introduction to Computer Methods Daene C. McKinney.
Lecture Set 12 Sequential Files and Structures Part B – Reading and Writing Sequential Files.
Visual C Sharp – File I/O - 1 Variables and arrays only temporary - lost when a program terminates Files used for long term storage (Data bases considered.
File Handling. Data Files Programs that can only get data from the user interface are limited. –data must be entered each time. –only small amounts of.
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
Using Arrays and File Handling
Chapter 9 Files I/O: Files, Records and Fields Part 3.
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
File I/O 11_file_processing.ppt
Chapter 10: Structures and Sequential Access Files
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
File I/O Static void Main( ) {... } data. Topics I/O Streams Reading and Writing Text Files Formatting Text Files Handling Stream Errors File Pointers.
Data files and databases. Need a control to browse to a file Standard controls for drive folder and list not much use The CommonDialogs control offers.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
11-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
Chapter 9 I/O Streams and Data Files
Chapter 10 Sequential Files and Structures. Class 10: Sequential Files Work with different types of sequential files Read sequential files based on the.
Reference: Lecturer Lecturer Reham O. Al-Abdul Jabba lectures for cap211 Files and Streams- I.
File I/O What We’ll Cover –Visual Basic Techniques for Text Files –.NET Techniques for Text Files What We’ll Not Cover –Binary File I/O –XML File I/O.
Tutorial 9: Sequential Access Files and Printing1 Tutorial 9 Sequential Access Files and Printing.
File Input and Output (I/O) Engineering 1D04, Teaching Session 7.
Copyright 2007, Paradigm Publishing Inc. ACCESS 2007 Chapter 3 BACKNEXTEND 3-1 LINKS TO OBJECTIVES Modify a Table – Add, Delete, Move Fields Modify a Table.
INPUT AND OUTPUT 1. OUTPUT TextBoxes & Labels MsgBox Function/Method MessageBox Class Files (next topic) Printing (later) 2.
Files and Streams. Objectives Learn about the classes that support file input/output Understand the concept of abstraction and how it related to the file.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 13 File Input and.
Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer.
Copyright © 2012 Pearson Education, Inc. Chapter 5 Loops, File, and Random Numbers.
6-1 Chapter 6 Working with Arrays in VB.NET. 6-2 Learning Objectives Understand the use of list and table arrays in VB.NET projects and the difference.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Ten Structures and Sequential Access Files.
Introduction to Files in VB Chapter 9.1, 9.3. Overview u Data Files  random access  sequential u Working with sequential files  open, read, write,
File IO.  File Input/Output  StreamWriter  StreamReader  Text Files  Binary Files.
BACS 287 File-Based Programming. BACS 287 Data Hierarchy  Database - Collection of files, relationships, integrity information, etc  Files - All records.
Error Trapping Exception Catching 1. Syntax & Compile-time  VB – Editor/Compiler Logic and Design  You Runtime  You Types of Errors: 2 When your program.
McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 11 Data Files.
Files and Streams. What is a file? Up until now, any stored data within a program is lost when the program closes. A file is a permanent way to store.
Object-Oriented Application Development Using VB.NET 1 Chapter 13 Introduction to Data Access Classes and Persistence.
Files and Streams. Objectives Learn about the classes that support file input/output Understand the concept of abstraction and how it related to the file.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
1 Displaying Dialog Boxes Kashef Mughal. 2 Midterm Stats Here we go  Average was  Low was 116  High was 184  Mid Quarter Grade - check any.
Reading from a file A file is typically stored on your computers hard drive. In the simplest case, lets just assume it is text. For a program to use.
INPUT AND OUTPUT.
Reading & writing to files
Files.
Using Multiple Forms.
Files and Streams.
Files and Streams Lect3 CT1411.
Sequential Input and Output using Text Files
Topics Introduction to File Input and Output
Chapter 3.5 Input and Output
Tutorial 9 Sequential Access Files and Printing
Files and Streams Lect10 GC201 12/1/2015.
CIS16 Application Development and Programming using Visual Basic.net
Topics Introduction to File Input and Output
Files and Streams.
Topics Introduction to File Input and Output
Chapter 11 Saving Data and Objects In Files
Presentation transcript:

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 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 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. 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 – 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, We’d get: strRecord(0) = “Anderson” strRecord(1) = “Andy” strRecord (2) = “39495” strRecord(3) = “ ”

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