BACS 287 File-Based Programming. BACS 287 Data Hierarchy  Database - Collection of files, relationships, integrity information, etc  Files - All records.

Slides:



Advertisements
Similar presentations
Files Organisation sequential files. Readings u Schneider Chapter 8 u Shelly Cashman to 9.14; to 9.11 u Meyer to 2-37; 1995.
Advertisements

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.
Lecture Roger Sutton CO331 Visual Programming 19: Simple file i/o Exceptions – Error handling 1.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Reading and Writing Files Keeping Data. Why do we use files? ä For permanently storing data. ä For dealing with information too large to fit in memory.
Chapter 9: Sequential Access Files and Printing
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files.
An Introduction to Programming with C++ Fifth Edition Chapter 13 Sequential Access Files.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 1 Introduction.
Chapter 8 - VB.Net by Schneider1 Chapter 8 – Sequential Files 8.1 Sequential Files Creating a Sequential File Adding Items to a Sequential File Error Trapping.
MIS316 – BUSINESS APPLICATION DEVELOPMENT – Chapter 14 – Files and Streams 1Microsoft Visual C# 2012, Fifth Edition.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 1 Introduction to Computers and Programming.
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.
CSC 125 Introduction to C++ Programming Chapter 1 Introduction to Computers and Programming.
1 Chapter 8 – Text Files 8.1 Managing Text Files 8.2 StreamReaders, StreamWriters, and Structured Exception Handling 8.3 XML.
1 Text Files Managing Text Files StreamReaders, StreamWriters, ReadAllLines, and WriteAllLines Method The OpenFileDialog Control Structured Exception Handling.
Chapter 8 - VB 2008 by Schneider1 Chapter 8 – Sequential Files 8.1 Sequential Files 8.2 Using Sequential Files.
Chapter 10: Structures and Sequential Access Files
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
Creating Sequential Files, Adding Data, & Deleting Data.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
Reference: Lecturer Lecturer Reham O. Al-Abdul Jabba lectures for cap211 Files and Streams- I.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 7 Files.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
1 COMP3100e Developing Microsoft.Net Applications for Windows (Visual Basic.Net) Class 6 COMP3100E.
Tutorial 9: Sequential Access Files and Printing1 Tutorial 9 Sequential Access Files and Printing.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
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.
© 1999, by Que Education and Training, Chapter 9, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
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.
Using Text Files in Excel File I/O Methods. Working With Text Files A file can be accessed in any of three ways: –Sequential access: By far the most common.
READING AND WRITING FILES. READING AND WRITING FILES SEQUENTIALLY  Two ways to read and write files  Sequentially and RA (Random Access  Sequential.
Chapter 11 File Processing. Objectives In this chapter, you will learn: –To be able to create, read, write and update files. –To become familiar with.
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.
Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access 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.
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.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
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.
Chapter 14: Sequential Access Files
Microsoft Visual Basic 2005: Reloaded Second Edition
TMF1414 Introduction to Programming
Files.
Appendix D: Network Model
Ch 10 Sequential Access Files
© 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.
Chapter 8 – Sequential Files
Files and Streams Lect3 CT1411.
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
Agenda Test next Week! SI or no SI? File Update Techniques – Review.
MSIS 670: Object-Oriented Software Engineering
Variable Review & IO User 12/26/2018.
Chapter 3.5 Input and Output
Visual Basic 6 Programming.
Tutorial 9 Sequential Access Files and Printing
Files and Streams Lect10 GC201 12/1/2015.
Fundamental of Programming (C)
CIS16 Application Development and Programming using Visual Basic.net
Input and Output.
Topics Introduction to File Input and Output
Presentation transcript:

BACS 287 File-Based Programming

BACS 287 Data Hierarchy  Database - Collection of files, relationships, integrity information, etc  Files - All records about the entity class  Records - Collection of fields about an entity  Fields - Named group of bytes associated with a characteristic of interest  Bytes - 8 bits, can store 1 character  Bits - Binary Digits (1,0)

BACS 287 File Processing  File processing manages data from bits up to files. (Database requires separate database management package).  File processing is most closely associated with the traditional approach to programming (i.e., procedural).  VB.NET can support the traditional approach as well as more modern approaches.

BACS 287 File Processing Basics  Data files are used as a way to make sure that the results of your program are available after the program finishes.  Permanent files are stored on disks.  VB directly supports 3 basic file organizations: – Sequential – text files in continuous blocks – Random – for fixed length files (text or binary) – Binary – for arbitrarily structured files

BACS 287 Sequential Data Files  Sequential data files store data in the order that it arrives.  New data is added to the ‘back’ of the file.  If you want to find a specific record, you must read all records from the start of the file.  This is efficient for some applications.

BACS 287 Sequential File Structure

StreamReaders & StreamWriters Sequential files can be processed as a “stream” of data StreamReaders are VB objects that read data from a stream StreamWriters are VB objects that write data to a stream Neither can be used before the file is opened BACS 287

Example StreamReader Statements Creating and reading from StreamReader Dim stVar as IO.StreamReader stVar = IO.File.OpenText(filespec) Dim StrVar = srVar.ReadLine srVar.EndOfStream SrVar.Close() BACS 287

StreamReaders Use strVar = srVar.ReadLine reads the line pointed to, assigns the line to the string variable strVar, and moves the pointer to the next line of the file. The value of srVar.EndOfStream will be True after the entire file has been read. The statement srVar.Close() terminates communication with the file. BACS 287

StreamReader Use If sr is a variable of type StreamReader, an entire text file can be read with a loop of the form Do while not sr.EndOfStream strVar = srVar.ReadLine. Loop BACS 287

Example StreamWriter Statements Creating and writing to StreamWriter Dim swVar as IO.StreamWriter swVar = IO.File.CreateText(filespec) swVar.WriteLIne(strData) swVar.Close() BACS 287

StreamWriter Use swVar.WriteLine(info) initally places the information into the first line of the file. Subsequent statements of that form place information into lines at the end of the file. The statement swVar.Close() terminates communication with the file. BACS 287

Text File Modes OpenText – open for input CreateText – open for output AppendText – open for append A file should not be opened in two different modes at the same time. BACS 287

Example StreamWriter Statements Adding to existing file via StreamWriter If (IO.File.Exists(filespec))… Dim swVar as IO.StreamWriter swVar = IO.File.AppendText(filespec) swVar.WriteLIne(strData) swVar.Close() BACS 287

StreamWriter Use 1. Execute the statement Dim swVar As IO.StreamWriter = _ IO.File.AppendText(filespec) where filespec identifies the file. 2. Add lines of data to the end of the file with the WriteLine method. 3. After all the data have been written into the file, close the file with swVar.Close(). Note: If the file does not exist, the AppendText method will create it. BACS 287

Avoiding Errors Attempting to open a non-existent file for input brings up a message box titled: FileNotFoundException There is a method to determine if a file exists before attempting to open it: IO.File.Exists(filespec) will return True if the file exists. BACS 287

Test for Existence of File Dim sr As IO.StreamReader If IO.File.Exists(filespec) Then sr = IO.File.OpenText(filespec) Else message = "Either no file has yet been " message &= "created or the file named" message &= filespec & " is not found." MessageBox.Show(message, "File Not Found") End If BACS 287

Deleting Data from a Sequential File An individual item of a sequential file cannot be changed or deleted directly. A new file must be created by reading each item from the original file and recording it, with the single item changed or deleted, into the new file. The old file is then erased, and the new file renamed with the name of the original file. BACS 287

Delete and Move Methods Delete method: IO.File.Delete(filespec) Move method (to change the filespec of a file): IO.File.Move(oldfilespec, newfilespec) Note: The IO.File.Delete and IO.File.Move methods cannot be used with open files. BACS 287

Random Data Files  Random data files are stored so you can access the record of interest directly (thus, random files support direct access).  Random access files have records of fixed length.  The records are defined using a special VB command called the ‘Type Statement’.

BACS 287 Random File Structure

BACS 287 VB Type Statement TYPE structurename fieldname1 AS datatype fieldname2 AS datatype... END TYPE TYPE employee Name as string * 30 Address as string * 50 Salary as integer END TYPE

BACS 287 VB File-Based Example Public Function Open_Data_File() Rem******************************************************************* Rem Routine to open a new data file on the user disk. lngRecLength Rem is used to define record length to open statement. Rem******************************************************************* Dim lngRecLength As Long ' determine the length of recRestaurant records lngRecLength = Len(recRestaurant) ' open a random access data file Open "d:\287\projects\p2\p2_test\p2data.fil" For Random As #1 Len = lngRecLength ' find the position of the last record in the data file lngLastRecord = LOF(1) / lngRecLength End Function

BACS 287 Random File Commands  There are a few commands that are needed to use random files in VB. – Type statement – Open – Close – Get – Put

BACS 287 Opening/Closing Random Files  You must open a random file before you can access its data Open data.fil for Random as #1 Len = 50  Likewise, you close it after you are finished Close #1  While it is open, you can add and modify records using the manipulation commands

BACS 287 Random File Manipulation  2 commands allow you to read file contents (get) and write contents (put).  You need 3 pieces of information for both the Get and Put commands – File number – Record position – Record work area

BACS 287 Random File Manipulation GET statement: GET filenumber, record position, work area PUT statement: PUT filenumber, record position, work area Examples: Get #1, Position, wrkRecord Put #1, Position, wrkRecord

BACS 287 Random File Manipulation