Download presentation
Presentation is loading. Please wait.
Published byHilary Logan Modified over 9 years ago
1
Irwin/McGraw-Hill Copyright© 2000 by the McGraw-Hill Companies, Inc. PowerPoint® Presentation to accompany prepared by James T. Perry University of San Diego
2
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Ch 10: Data Files Create data files –Reading, writing, opening, closing Differentiate sequential & random files Trap user errors and handle them Fixed length strings in user data types Read/Write random data files Updating a random file Using the InputBox function
3
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Data Files Data files consist of records & fields Record key determines a record’s position in a file Two common file organizations are sequential and random Process a file by 1. opening the file 2. processing the data, 3. closing the file
4
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill FreeFile and Close Statements FreeFile function returns the next unused file number –intFileNumber = FreeFile –In large projects, file numbers are hard to track and allocate Close issued automatically when you end Issue Close statement before leaving prog. –Close #1 or Close #1, #2
5
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Sequential File Organization Sequential file records are stored one after another Use Write to output data Use Input to read data File must be opened prior to first use Form: Write #fn, item 1, item 2,…, item n Input #fn, item 1, item 2,…, item n EOF function signals end of file
6
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Writing to a Sequential File Write statement places data in output Form: Write #file, o 1, o 2, …, o n You can separate fields with commas or semicolons Output fields are separated with commas, and strings are quoted. / ends each record Write #1, txtLname.Text, 54.89
7
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Reading Data in a Sequential File Form: Input #fn, item 1,…,item n Separate fields items with commas File number must be that of an open file When you read the last record, end-of-file signals You detect end of file with the EOF function: EOF(FileNumber)
8
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Trapping Program Errors Errors may be trapped asynchronously Visual Basic generates an error number whenever an error occurs To handle errors, you must –Turn on error handling feature: On Error... –Create error handling code –Determine what is to be done after processing the error (continue, end program,…)
9
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill The Err Object The Err object holds information about error that just occurred Err.Source holds the source of the error Err.Number holds the error number Err.Description contains error description You can raise an error condition—turn on an error—with: Err.Raise Number:=xx
10
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Coding Error-Handling Routines On Error statement designates error handler Code to handle errors follows line label Line label appears on line by itself and ends with a colon A Case statement works well to sort out errors with a "switchboard" Continue execution with Resume statement
11
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Exit and Exit Sub Statements Exit Sub immediately exits current sub procedure Exit Function immediately exits current function Exit is used to prematurely halt execution of sub procedure or function when extraordinary conditions occur Place Exit Sub above error handling label
12
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Saving Changes to a File When data changes, ask users if they want to save the changes before program ends Changed data is known as “dirty” data Keep track of data changes with a global boolean flag Any procedure that allows data to change should set the flag to True—indicating “dirty” data file Check file just before ending program
13
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Sequential File Prog. Example You can load a combo box by reading from a data file and executing the AddItem method Reading file & list filling halts when EOF occurs on input file (See Hands On Programming example)
14
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Random data files You can read/write data in any order Open “filename” For Random As #1 LEN=x Get #filenumber, [record#], RecordName RecordName is a user-defined data type: Type FullName strLastName As String * 20 strFirstName As String * 15 End Type
15
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Random data files Output: Put Put #filenumber, [recordnumber], Recordname Put #1, iRecordNo, pCustomerRecord Get & Put statements read an entire record You refer to fields in user-defined structure by recordname.fieldname lstName.AddItem mRec.LastName end of file is calculated via record lengths
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.