File I/O in Visual Basic

CIS241 - Fall 2001

 

This is a very basic introduction to file i/o in VB.  To augment this information, peruse Chapter 14 of Hartman’s book.  Also, refer to the online Help topics mentioned below.

 

File Types

 

The three most basic file types in VB are sequential, random and binary.  Sequential files are accessed in a sequential manner.  Both reading and writing must be done in order.  Random files, by contrast, can be accessed at any specified location.  In order for this to happen, though, the material stored must be organized as a record, with all records stored in the file being the same length.  Binary files can be thought of as undifferentiated bit strings.  This gives great flexibility, but also places responsibility on the programmer to keep track of what is being read and stored.

 

For more detailed information look up sequential access files in VB Help.

 

Open/Close

 

As in most other languages, in VB files must be opened before access and closed afterwards.  This is accomplished via the Open and Close statements.  Although most languages provide for using an identifier as an internal name for referring to an opened file, VB insists that you use a number.  In addition, at the time the file is opened it must be specified how the file is to be used.  Some examples of Open and Close statements are below.

 

            Open App.Path & "\record.txt" For Output As #1

 

            Open App.Path & "\trans.txt" For Input As #2

 

            Open App.Path & "\simp" For Random As #3 Len = Len(var)

 

Some comments are in order.  Open is followed by the name of the file to be opened.  App.Path gives the pathname of the directory from which the application program is being executed.  This is concatenated, using the concatenation operator &, to the name of the file in that directory.  Notice that the file name must be preceded by the backslash symbol ( \ ).

 

In the program that follows, the file is identified by the number under which it was opened.  Obviously, the programmer must ensure that each file is opened under a unique number.  The type of access is specified as either For Input, For Output, For Random or For Binary.

 

When a random access file is opened, the length of the records it contains must be specified using Len = . . .  Often that length can be obtained by using the Len function which returns the length of its argument (in this case a record).

 

For more detailed information look up Open statement, Close statement, App object, Path property, Len function, and Len keyword  in VB Help.

 

Input/Print/Write; Get/Put

 

Sequential files may be written to with either Print # or Write #.  For reading, however, only Input # is used.  Both random access and binary files are written with Put # and read using Get #.  Some examples of Print and Input statements are below.

 

            Print #1, "Conversion results”, cval + 7

 

            Print #1, "Wolfram Number = "; gWnum

 

            Print #1, "111 ==> "; Bin(7)

 

            Print #1,

 

            Input #1, numcells

 

            Input #1, numstates

 

            Input #1, cellval, cellnum

 

Some comments are in order.  A comma must follow the file number.  Items printed may be separated by commas or semicolons.  If separated by commas, they are printed in separate print fields.  Items to be printed can be string constants, numeric constants, variables, or expressions.  One way to force a line feed is to issue a print without any items to print.  To read from a file, use Input # followed by a list of variables into which the values from the file are to be placed.

 

For more detailed information look up Print #, Write #, Input #, Put #, and Get # in VB Help.  Some of these are hard to find in the Index.  One way to get to their descriptions is to highlight the word in the code window, then press F1 for context sensitive help.