File Handling
The usual model for all computer programs is that a user inputs data and
the computer saves it. Okay, there's a lot more than that to a computer
program, but the point is that virtually every program is written to save
data that is entered or data that is calculated by the software itself. If
you want to be an expert VB programmer then you'll need to know a lot about
how VB handles files - what is possible and what is not, plus how to do
something in the smallest code or in the least amount of time. To do this
you need to understand the file handling features which VB offers.
Sequential (Text) Files
In VB, Microsoft has separated the file handling capabilities from the
database handling features. With databases, the file manipulation is
pretty much transparent to the user. However, in non-database applications
the programmer has to handle virutally all aspects of reading or editing
the data contained in a file.
Because of their universally standard format, simple text files (also called
sequential files) are often used as the storage method for information.
The old DOS Edit program and the newer windows NotePad programs both create
simple text files.
To simplify the handling of text files, Microsoft is working on a new set
of features that will be implemented in
an ActiveX object (the FileSystemObject). Until then, file handling will
continue as the manual procedure that it is today.
The two statements you must use to access a text file are OPEN and
CLOSE. Here's a quick example:
OPEN "filename" for INPUT as #1
CLOSE #1
In this example nothing was done but opening the file and closing it.
Here's a little more useful example:
OPEN "filename" FOR INPUT as #1
WHILE NOT Eof(1)
LINE INPUT #1, temp$
WEND
CLOSE #1
In this example, the code reads one line at a time, walking through the
file until the end-of-file (EOF) is true. The line of data (minus the
carriage return / line feed characters) is put into the string variable
temp$. In your own applications you can save each line into an array, or
concatenate them into a single string variable, or any other processing
you want to do.
This next example shows how the text can be read as a single string
variable and put into a textbox control.
OPEN "filename" FOR INPUT as #1
WHILE NOT EOF(1)
LINE INPUT #1, temp$
alltext$ = alltext$ & temp$ & vbcrlf
WEND
textbox1.text = alltext$
CLOSE #1
Note that I had to add back in the carriage return / line feed to maintain
the same line break as was in the original file. If you want the file to
be read simply as a long string, replace the vbcrlf with a space (" ") to
keep separation between the word at the end of the line and the word which
starts the next line.fx
The examples so far were to read a text file. Here's an example for writing
to a text file:
OPEN "filename" FOR OUTPUT AS #1
For i = 1 to 10
PRINT #1, i
NEXT i
CLOSE #1
In this case, each PRINT operation goes to a different line in the text file
(which means a crlf character is inserted into the byte stream). The PRINT
statement provides a variety of options for putting the data into the
output file. You can put all the data in one line, separate each piece by
one or more characters, or format the numbers as they are written. The
bottom line is that whatever is written to the file is written as text
and when it's read back your program must convert it to numbers as needed.
One more example will be worthwhile. Suppose the data consists of columns,
and you next to extract the numbers from those columns. How do you do it?
Here's how it could be done for the case of having 3 numbers per line, 1 in
each ten columns:
OPEN "filename" FOR INPUT as #1
WHILE NOT Eof(1)
LINE INPUT #1, temp$
Number1 = VAL (mid$(temp$, 1, 10))
Number2 = VAL (mid$(temp$, 11, 10))
Number3 = VAL (mid$(temp$, 21, 10))
WEND
CLOSE #1
Binary (Data) Files
Binary files are created by storing variables (both string and/or numeric)
without any added formatting as can be done with sequential file. An integer
variable can be written, followed by a string, and followed by another
integer. However, the programmer has to keep track of which variable data
is placed where!
One of the really great features of binary file access is that you can often
avoid using the VB database features. This is important because the
distribution files for database access are HUGE and I hate having to
put them in my application distribution files.
Another especially nice feature of binary files is that you can PUT a complete
user-defined variable in a single statement, and recover it just as easily.
This greatly simplifies data storage in many cases.
Let's get right into some examples. To put data into a file, use this:
OPEN "filename" for BINARY as #1
PUT #1, var1, var2, var3
CLOSE #1
In this example, all we did was write three variables to the file. To read
them back, use this:
OPEN "filename" for BINARY as #1
GET #1, var1, var2, var3
CLOSE #1
You'll notice that we didn't have to format the PUT or GET statements in
any way.
|