Presentation is loading. Please wait.

Presentation is loading. Please wait.

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,

Similar presentations


Presentation on theme: "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,"— Presentation transcript:

1 Introduction to Files in VB Chapter 9.1, 9.3

2 Overview u Data Files  random access  sequential u Working with sequential files  open, read, write, close, append, eof u Line based sequential file access u File system details  Current directory, relative path, absolute path u Common Dialog control

3 VB - Persistence in software u Information entered into a program is volatile  Can switch computer off  Power can be removed accidentally  Power-cuts can occur  Computer System can crash u We need to make data persist beyond the execution of a program

4 Files u In business administration, a file is used to keep paper documents together in an orderly way u In a computer, a file stores data in an order way  items are written to the file in strict sequence  items are read from the file in the same sequence u The medium used to hold files is non-volatile  data in a file persists when the computer is off

5 Computer file mechanisms u Programming languages provide commands to access files  open, read, write, append, close u The operating system maintains a link between a program and an open file using a file reference or handle:  An integer value by which the file can be identified

6 Program Files vs Data Files u A program file contains the instructions that both create the user interface and tell the objects how to respond to events  Also called an executable file u A data file is a collection of information of a specific category, or format

7 File Storage Hierarchy u A file system is a collection of files files u Files can be viewed as sets of records u Records are sets of fields u Fields are variable types File Record Field 1 Field 2 Field 3 Field 4 Field 5

8 Record-based File Types u Random  Fixed record length  Indexed records  Fast access  Indices may limit accessibility u Example  CD recording can go right to the song you want  Library card file tells you exactly where the book is u Sequential  Does not have to be fixed length  No indexing overhead  Easily transferable u Example  Cassette tape must role through the tape to find correct place

9 Sequential Access Data Files u Similar to a cassette tape in that each record in the file is both stored and retrieved in consecutive order u Advantage: easy to create u Disadvantage: you can process the records only in the order in which they were entered u Use for small files, files consisting only of text, or files of records that will always be accessed in sequential order

10 Open Statement u Open pathname For mode As # filenumber  pathname is the name of the file you want to open; it should include the drive letter and path  a String variable  a String literal, wrapped in quotes  mode can be either Input, Output, or Append  filenumber is a number that you assign to the file, and it must be an integer between 1 and 511, inclusive  could be an integer variable

11 Sequential Access File Modes u Input - you open a file for input when you want to read the file u Output - you open a file for output when you want to create a new file and then write data to it u Append - you open a file for append when you want to add data to the end of an existing file

12 Open File Pointer u When you open a file for input, the open file pointer is positioned at the beginning of the file, immediately before the first character u When you open a file for output, the open file pointer is positioned at the beginning of the empty file u When you open a file for append, the open file pointer is positioned immediately after the last character in the file

13 Write # Statement u Write # filenumber, [outputlist]  filenumber is the number used in the Open statement to open the file  outputlist is one or more numeric or string expressions, separated by commas

14 Close Statement u Close [# filenumber]  filenumber is the number used in the Open statement to open the file  A Close statement with no filenumber closes all open files

15 Code to create a file Sub CreateFile( ) Dim intFile As Integer ' File handle Dim strName As String Dim intNumber As Integer intFile = FreeFile Open "MyFile.dat" For Output As #intFile strName = InputBox("Name?") Write #intFile, strName intNumber = val(InputBox("Number?")) Write #intFile, intNumber Close #intFile End Sub Sub CreateFile( ) Dim intFile As Integer ' File handle Dim strName As String Dim intNumber As Integer intFile = FreeFile Open "MyFile.dat" For Output As #intFile strName = InputBox("Name?") Write #intFile, strName intNumber = val(InputBox("Number?")) Write #intFile, intNumber Close #intFile End Sub

16 Inspecting a Sequential Access File u Use a basic word processor or text editor u Each line in the sequential access file represents a record u Fields in the record are separated by commas u String fields are enclosed in quotation marks

17 EOF Function u EOF(filenumber)  filenumber is the number used in the Open statement to open the file  EOF stands for “end of file”  returns the Boolean value True if the record pointer is at the end of the file (after the last record); otherwise it returns the Boolean value False  Do While Not EOF(1)  Do Until EOF(1)

18 Input # Statement u Input # filenumber, variablelist  filenumber is the number used in the Open statement to open the file  variablelist is one or more numeric or string variables, separated by commas  each variable in the variablelist is associated with a field in the record  the number, data type, and order of the variables in the variablelist must match the fields in the record

19 Code to retrieve data from a file Sub ReadFile( ) Dim intFile As Integer ' File handle Dim strName As String Dim intNumber As Integer intFile = FreeFile Open "MyFile.dat" For Input As #intFile Input #intFile, strName MsgBox strName Input #intFile, intNumber MsgBox intNumber Close #intFile End Sub Sub ReadFile( ) Dim intFile As Integer ' File handle Dim strName As String Dim intNumber As Integer intFile = FreeFile Open "MyFile.dat" For Input As #intFile Input #intFile, strName MsgBox strName Input #intFile, intNumber MsgBox intNumber Close #intFile End Sub

20 Another Code Example u Dim intFileNum As Integer Private Sub cmdSave_Click() intFileNum = FreeFile Open "X:\demo.txt" For Append As #intFileNum Write #intFileNum, txtIn(0).Text, Val(txtIn(1).Text), txtIn(2).Text Close #intFileNum End Sub

21 Line-based File I/O Line Input #intFileNumber, strLineOfText Print #intFileNumber, strLineOfText u For output, the line of text may need to be assembled from various components using string concatenation u Text Error: in the code example on page 307 "Print #1" should be "Print #intStudentFile"

22 Windows File System u Drive  A physical data storage device, such as a hard disk, floppy, or cd  Each drive is assigned a letter  On most systems, the primary hard drive is 'C', and the floppy drive is 'A'  In the CS labs, your personal data is stored on "I", a network drive u File  Files contain data

23 Windows File System u Directory  Directories are used to organize files  A directory can contain other directories, or files  A drive can also contain directories or files u Current directory  Whatever directory you are currently "in"  Initially, the current directory is the place VB is stored  You may not have permission to create files in this directory  You can find the current directory using the CurDir function

24 Typical Windows file system C: Program Files temp WindowsDocuments And Setting AdministratorAll UsersDefault User DesktopCookiesStart Menu Hello.txt

25 Absolute Pathnames u To use a file in VB, you need more than its name  To access a file in a directory other than the current one, you need to know its pathname u A pathname is an address that shows the file’s position in the file system u Absolute path names give a files location in relation to the top of the file system structure  All absolute path names begin with a drive letter  Also called full path names

26 What is the full path of each hello.txt? C: Program Files temp WindowsDocuments And Setting AdministratorAll UsersDefault User DesktopCookiesStart Menu Hello.txt

27 Common Dialog Box u Common Dialog Box – provides the standard MS application interface for choosing a file from the disk. Components Menu from Project Menu; choose MS Common Dialog Control u Icon placed on form, but is not seen in runtime u Properties  CancelError – what to do if no file selected  DialogTitle – hint to user at what file you want  Filter – what default file types to search for  InitDir – what directory to look in first


Download ppt "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,"

Similar presentations


Ads by Google