Programming:Visual Basic Classic/Files
Another essential part of Visual Basic is file Input/Output, that is, working with files. While programming, you may want to at some point save data so they may be accessible for further use. This is where file I/O comes in. VB allows us to perform most operations available in Windows Explorer and DOS command line. Before you can do this, you need to understand how file I/O work.
Layout of Data
VB generally arranges data into records and fields for the programmer's convenience. The best way to explain this is by illustrating it:
This is obviously data containing names. Each line of data is called a
record. So the first record contains Bob, Larry, and George. A field is each
item in a record. So the first field in the first record would be Bob, the
second field would be Larry, and so on. There may be as many fields and records
as you see fit.
Records and fields are created really just for the programmer's convenience. For greater flexibility, you can also view the whole thing as a string. And you can split the string into records and fields yourself with the built-in Split() function.
When a program recieves data from an outside source, it is considered input. In Visual Basic this source is usually a standard text file, with a .txt file extension(as which could be read with NotePad). First, you need to open the file from which you want to read the data. This is done by the following:
Open <filename> For <mode> As <channelnumber>
|Open "c:\filename.txt" For Input As #1|
The file path can be anything you want, if it doesn't exist, a new file (and directory(s)) will be created. The extension of the file doesn't matter much. It will not affect the content of the file nor the process of writing/reading the file. So you can also do this:
|Open "c:\filename.myfile" For Input As #1|
|Open "c:\filename" For Input As #1|
The open and file path are self explanatory. However, "for input" states it is being used to recieve data from an outside file, VB is "inputting" data from a file into variables . When outputting(writing to a file), this is changed to "for output". When we want to open a file in binary, it's "for binary". "As #1" is which channel is being used. When you are seeking specific information from the file you would use "for random". A channel could be seen as Visual Basic setting up a pathway to the file. Only one file can be used on a channel at a time. So if you want to open multiple files without closing any, you can use different channels. To input a line of data, it is fairly simply:
Input <channel>, <variables>
|Input #1, x, y, z|
This will write data to whatever channel 1 is connected to. It will store the
first field of the current record into X, and the second field into Y, and the
third into Z.
Finally, we should ALWAYS close the file after the work is done. The syntax is simple:
As an example:
More on using input will be covered in the "Use of Data" section.
Note: If the code will be part of a larger program, the channel number should be obtained in a dynamic way instead of a hard-coded number. This is done using the function FreeFile()
|Dim nLogFile As Long
nLogFile = FreeFile()
Open "c:\filename" For Input As #nLogFile
Output is very similar to input. The only difference is, well, it sends information OUT of the program and into a file. Once again, we shall only use text files (.txt). First you must open the file:
|Open "C:\filepath.txt" For Output As #1|
You may have noticed, the only thing that differs from opening a file for input, is the "For Output". Once this is done, everything else is simple.
|Write #1, x, 5|
Assuming X = 3, if you open the text document it will look like:
You can place anything to write, however, it is important to note when inserting a string, it should be placed in quotes. If it is a variable containing a string, this is done automatically. Another important note is that two consecutive write statements will write to seperate lines. For example:
|Write #1, 5, 4
Write #1, 7, 4
Will appear in the file as
5, 4 7, 4
Keep in mind, when outputting to a file, it will ERASE all prior information in the text. To add on to existed information, the "Append" command must be used.
If, during runtime, the file at hand was modified, and a new line was added which contained necessary data, and the open statement ended as "#1", whatever was in line 1 of the file would be written over(only if the file was opened as Output). To solve this, a variable is dimmed as FreeFile (Dim FF as FreeFile). This will open the first free slot or line in the file at hand. The new Open statement would become:
|Open "C:\filepath.txt" For Output As FF|
(will erase file)
|Open "C:\filepath.txt" For Append As FF|
(will not erase file)
Using the second statement, the first free line in the file will be written to.