Introduction to Files in VB - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Introduction to Files in VB

Description:

a String literal, wrapped in quotes. mode can be either Input, Output, or Append ... String fields are enclosed in quotation marks. EOF Function. EOF(filenumber) ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 28
Provided by: jayso
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Files in VB


1
Introduction to Files in VB
Chapter 9.1, 9.3
2
Overview
  • Data Files
  • random access
  • sequential
  • Working with sequential files
  • open, read, write, close, append, eof
  • Line based sequential file access
  • File system details
  • Current directory, relative path, absolute path
  • Common Dialog control

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

4
Files
  • In business administration, a file is used to
    keep paper documents together in an orderly way
  • 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
  • The medium used to hold files is non-volatile
  • data in a file persists when the computer is off

5
Computer file mechanisms
  • Programming languages provide commands to access
    files
  • open, read, write, append, close
  • 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
  • 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
  • A data file is a collection of information of a
    specific category, or format

7
Storage Hierarchy
  • A file system is a collection of files files
  • Files can be viewed as sets of records
  • Records are sets of fields
  • Fields are variable types

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

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

10
Open Statement
  • 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
  • Input - you open a file for input when you want
    to read the file
  • Output - you open a file for output when you want
    to create a new file and then write data to it
  • Append - you open a file for append when you want
    to add data to the end of an existing file

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

13
Write Statement
  • 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
  • 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
16
Inspecting a Sequential Access File
  • Use a basic word processor or text editor
  • Each line in the sequential access file
    represents a record
  • Fields in the record are separated by commas
  • String fields are enclosed in quotation marks

17
EOF Function
  • 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
  • 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
20
Another Code Example
  • Dim intFileNum As IntegerPrivate Sub
    cmdSave_Click()intFileNum FreeFileOpen
    "X\demo.txt" For Append As intFileNumWrite
    intFileNum, txtIn(0).Text, Val(txtIn(1).Text),
    txtIn(2).TextClose intFileNumEnd Sub

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

22
Windows File System
  • 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
  • File
  • Files contain data

23
Windows File System
  • Directory
  • Directories are used to organize files
  • A directory can contain other directories, or
    files
  • A drive can also contain directories or files
  • 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
Windows
Documents And Setting
temp
Administrator
All Users
Default User
Desktop
Cookies
Start Menu
Hello.txt
25
Absolute Pathnames
  • 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
  • A pathname is an address that shows the files
    position in the file system
  • 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
Windows
Documents And Setting
temp
Hello.txt
Administrator
All Users
Default User
Desktop
Cookies
Start Menu
Hello.txt
Hello.txt
27
Common Dialog Box
  • 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
  • Icon placed on form, but is not seen in runtime
  • 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
Write a Comment
User Comments (0)
About PowerShow.com