Reading from files and writing to them
If you open the file for sequential input or append access, you can use the Input function to read a specified number of characters into a String (or Variant) variable. For example, you can use the Input function in conjunction with the LOF function, which returns the length of an open file, to read the entire file (up to 32,000 characters) into a String variable:
fileNumber% = FreeFile
Open "DATA.DAT" For Input As fileNumber%
fileContents$ = Input(LOF(fileNumber%), fileNumber%)
To write an extended unstructured string rather than a fixed-length or variable-length record to a text file, you can open the file for binary access and use a Put statement. The following Put statement writes over the previous contents of a text file starting at the first byte. If the new string is shorter than the previous contents, the Put operation does not write over to the end of the file.
Open "DATA.DAT" For Binary Access Write As fileNumber%
Put fileNumber%, 1, fileContents$
If a file contains variable-length records, use the Input # and Write # statements to read and write records. The Input # statement reads a record into a list of variables, and the Write # statement writes to a file from a list of variables. Write # statements delimit and format entries so that they can be read by Input # statements. In both cases, the list of variables may be the members of a user-defined data type variable.
The following example reads each record from SCORES.DAT into a variable-length user-defined data type variable. If the student's score is at least 92, the script writes the record to HISCORES.DAT. The process continues until the EOF function returns TRUE (-1), indicating that the script has reached the end of SCORES.DAT.
Type Student
ID As Long
Name As String ' Variable-length string variable
Score As Single
End Type
Dim undergrad As Student
Sub WriteGoodStudents
Dim fileNum1 As Integer, fileNum2 As Integer
fileNum1% = FreeFile
Open "SCORES.DAT" For Input As fileNum1%
fileNum2% = FreeFile
Open "HISCORES.DAT" For Append As fileNum2%
While Not EOF(fileNum1%) ' Read until end of file.
Input #fileNum1%, undergrad.ID, undergrad.Name, undergrad.Score
If undergrad.Score > 92 Then
Write #fileNum2%, undergrad.ID, undergrad.Name, undergrad.Score
End If
Wend
Close fileNum1%
Close fileNum2%
End Sub
You can also use a Print # statement to write to a sequential text file, but Print # does not delimit and format the record to ensure that it can be read with an Input # statement.
When you are using sequential access to write to a file, you can open the file in input mode (replace the previous contents of the file) or append to the file. You cannot insert or replace text in the middle of the file.
You can also use the Line Input # statement to read each line into a String variable. Write # and Print # statements put a newline character at the end of each operation, so lines normally correspond to variable-length records (unless you write multi-line strings).
When you open a file for random or binary access, the file position is 1 (the first record or byte). Use a Get statement to read data into a variable, and use the Put statement to write data from a variable to the file. The variable may be a user-defined data type variable. Each Get and Put operation advances the file position accordingly. You can use the Seek statement to set the file position to a fixed-length record (random access) or to a byte (binary access). To get the current file position, use the Seek function.
Here is a revision of the preceding example, using fixed-length records and random access. Performance is better and numeric information is stored as such (rather than as strings), but the fixed-length string takes up a little extra space in each record.
Type Student
ID As Long
Name As String * 20 ' Fixed-length string variable.
Score As Single
End Type
Dim undergrad As Student
Sub WriteGoodStudents
Dim fileNum1 As Integer, fileNum2 As Integer
fileNum1% = FreeFile
Open "TESTSCORES.DAT" For Random Access Read As fileNum1% _
Len = Len(undergrad)
fileNum2% = FreeFile
Open "GOODSCORES.DAT" For Random Access Write _
As fileNum2% Len = Len(undergrad)
While Not EOF(fileNum1%) ' Read until end of file.
Get #fileNum1%,, undergrad
If undergrad.Score > 92 Then
Put #fileNum2%,, undergrad
End If
Wend
Close fileNum1%
Close fileNum2%
End Sub