Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "BACS 287 File-Based Programming. BACS 287 Data Hierarchy  Database - Collection of files, relationships, integrity information, etc  Files - All records."— Presentation transcript:

1 BACS 287 File-Based Programming

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

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

4 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

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

6 BACS 287 Sequential File Structure

7 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

8 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

9 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

10 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

11 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

12 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

13 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

14 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

15 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

16 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

17 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

18 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

19 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

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

21 BACS 287 Random File Structure

22 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

23 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

24 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

25 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

26 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

27 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

28 BACS 287 Random File Manipulation


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

Similar presentations


Ads by Google