<pre>Option Explicit
Function test()
' This vehicle demonstrates a facet of writing binray to a text file.
' The file is seen to grow to the length of the longest string ever written to it.
Dim intFile As Integer
intFile = FreeFile
' Test assumes clean environment, no other files open,_
' no file left open from a previous test.
If intFile <> 1 Then MsgBox "some error 1"
' If you don't have a C:temp this will cause an error.
Open "c:temperaseme.txt" For Binary As intFile
Dim strBuffer As String
' first write
' Assumes the file did not exist prior to this test.
' (See the KILL at the end of the procedure)
' Prepare a data record 10 characters in length, write it, _
' report the length of the file.
strBuffer = "0123456789"
Put #intFile, 1, strBuffer
MsgBox LOF(intFile) ' should return "10"; returns "11" if you didn't KILL the file.
' second write
' Assumes the file exists.
' Prepare a data record 11 characters in length, write it, _
' report the length of the file.
strBuffer = "01234567890"
Put #intFile, 1, strBuffer
MsgBox LOF(intFile) ' should return "11"
' third write
' Assumes the file exists.
' Prepare a data record 4 characters in length, _
' write it to the middle of the file, report the length of the file.
strBuffer = "0123"
Put #intFile, 3, strBuffer
MsgBox LOF(intFile) ' should return "11", because we have not pushed past the previous length of the file.
' Now display the string of the entire file.
' Note the embedded "0123"
Dim strFileString As String
strFileString = String$(LOF(intFile), " ") ' create buffer for GET routine
Get #intFile, 1, strFileString
MsgBox strFileString
Close intFile
' Now demonstrate that by KILLing the file and creating a new file, _
' the length can be reduced
Kill "c:temperaseme.txt"
intFile = FreeFile
If intFile <> 1 Then MsgBox "some error 1"
Open "c:temperaseme.txt" For Binary As intFile
' fourth write
' Assumes the file did not exist prior to this test.
' (See the KILL at the end of the procedure)
' Prepare a data record 10 characters in length, _
' write it, report the length of the file.
strBuffer = "0123456789"
Put #intFile, 1, strBuffer
MsgBox LOF(intFile) ' should return "10"; returns "11" if you didn't KILL the file.
' Now display the string of the entire file.
strFileString = String$(LOF(intFile), " ") ' create buffer for GET routine
Get #intFile, 1, strFileString
MsgBox strFileString
Close intFile
Kill "c:temperaseme.txt"
End Function
</pre>



