Transcript Document
Visual Basic 6 Programming. Lecture 6 : February 2005 Dr. Andrew Paul Myers 18/07/2015 1 Week 6 – Data Files (A short addendum on arrays). Why use data files? File types. Dealing with the filing system. Opening and closing files. Reading from and writing to files. Error trapping. 18/07/2015 2 More on Arrays. Arrays in VB can be multidimensional. Dim sngData1(50,50) As Integer Dim strWords (10, 10, 1 To 20) As String How many elements are there in each of these arrays? 18/07/2015 3 More on Arrays. You can find the bounds of these arrays as for a one-dimensional array. UBound(strWords, 1) LBound(strWords, 2) UBound(strWords, 3) 18/07/2015 gives 10 gives 0 gives 20 4 More on Arrays. Dim sngValues(5,5) As Single Dim intInner As Integer Dim intOuter As Integer For intOuter = 0 To 5 For intInner = 0 To 5 sngValues(intInner,intOuter) = 0 Next intInner Next intOuter 18/07/2015 5 Why use data files? Programming can be slow and painful. Programs are useful if they can be used repetitively for different data. Putting data in the code is very restrictive. Typing in numbers to a textbox is little better. Data is often available in digital form from a source. 18/07/2015 6 Data File Types. File Contents: Text files (ASCII codes). Comma Separated Variable (CSV) files, a type of text file. Binary files. Written for compactness of data and speed. File Access: Sequential. Random Access (only applies to binary files). 18/07/2015 7 Sequential Files. Analogous to reading a book, using your finger (or file pointer) to point to the letters as you read them: 18/07/2015 Start at the beginning. Read data sequentially. Advance the file pointer. Data finishes at end of file ( EOF ) marker. Can only safely add new information at the end of the data file. 8 File Handling. VB has 6 file commands which interface directly to the Windows OS, to affect files and folders Change drive, or change Directory: ChDrive <drive letter> ChDir <path> Make or Remove a directory: MkDir <dir name> RmDir <file name> Rename or delete a file: Name <file name> <new filename> Kill <file name> 18/07/2015 9 Examples. ChDrive “D:” ChDir “Data” MkDir “C:\Test” Name strFile1 strFile2 Kill “*.tmp” Warning: Don’t use these unless you really need to. Better to prepare files before you run your VB program! 18/07/2015 10 Opening a Data File. General form: Open <file_name> For <mode> As #<channel> Where: <file_name> is any valid file path as a text string. <mode> can be either Input, Output or Append. <channel> is an integer number (or variable). 18/07/2015 11 Opening a Data File. Examples: Open “Data.txt” For Input As #1 Open “Z:\my Documents\Data.txt” For Output As #2 Open strFile_Name For Output As #intChannelNumber 18/07/2015 12 Finished with a Data File? Data files must be closed when finished with! Close #<channel> Example: Close #1 “Close” on its own closes any and all open files. It is a good idea to include this in your “Exit” subprogram to tidy up any forgotten files. If there are no open files, “Close” does not create an error. 18/07/2015 13 Writing to a File. General Form: Print #<channel> , <data> Examples: Print #1 , “Hello” Print #intFNum , strName, sngValue, dblData 18/07/2015 14 Reading from a File. General Form: <mode> Input #<channel> , <data> e.g. Input #1 , strData Line Input #1 , strData Input #intFNum , strName, sngValue, dblData 18/07/2015 15 Choosing a Channel. If you are using multiple files, VB has a command to make choosing a free file channel easier! The “free file” command. Example: Dim intFree As Integer intFree = FreeFile Open strFile_Name For Output As #intFree Close #intFree 18/07/2015 16 End of File Marker. The end of a data file is indicated by a EOF marker. intDataNum = 0 Do While Not EOF(intFree) Input #intFree, dblData intDataNum = intDataNum + 1 Loop This is really useful (HINT) when reading any files. You do not need to know how much data is in the file in advance. 18/07/2015 17 Reading Characters. VB allows you to read any number of characters from a file. <variable> = Input ( <no. of chars> , <channel> ) e.g. Read 6 characters from a file. Dim strData As String strData = Input(6, #intFree) 18/07/2015 18 Length of a Data File. VB allows you to determine the length of a file in characters. i.e. <variable> = LOF(<channel>) Dim intData As Integer intData = LOF(1) 18/07/2015 19 Length of a Data File. Dim strContents As String strContents = Input(LOF(1),#1) This would read the whole data file and store it as one LARGE string. I wouldn’t do this if I were you! Inherently very dangerous. 18/07/2015 20 File Error Trapping. The only time we will ask you to use the dreaded “GoTo” statement! NEVER USE “GoTo” ANYWHERE ELSE! General form: On Error GoTo <label> A label has the form of a character string (no spaces) ending with a colon. Example: This_Is_A_Label: 18/07/2015 21 The Code. On Error GoTo FileError Rest of code etc... FileError: Select Case Err.Number Case 53 Print “File not found!” Case 55 Print “File already open!” etc... Note the colon is only used where the label is defined. 18/07/2015 22