The most efficient way to read the text file

Author:Anonymous    Updated:2008-10-16 12:20:58
Read the text of the document is the most efficient way to use the Input $ function, as the course of the following:
Function FileText (filename $) As String
Dim handle As Integer
handle = FreeFile
Open filename $ For Input As # handle
FileText = Input $ (LOF (handle), handle)
Close # handle
End Function
The use of the above-mentioned method than the use of Input command to read each line of the document much faster method. The following is the application of this function to read the contents of the Autoexec.bat many examples of the control line textbox:
Text1.Text = FileText (\ "c: \ \ autoexec.bat \")
However, please note: When the file contains Ctrl-Z (EOF) character, the above code may be a function of error. Therefore, it is necessary to amend the wording of the code:
Function FileText (ByVal filename As String) As String
Dim handle As Integer
'File to determine the existence of
If Len (Dir $ (filename)) = 0 Then
Err.Raise 53 'file not found
End If
'Binary mode to open the file
handle = FreeFile
Open filename $ For Binary As # handle
'Read the contents of the files closed
FileText = Space $ (LOF (handle))
Get # handle,, FileText
Close # handle
End Function
Previous:How to get the drive letter?-Vb Guide
Next:VB to use ASP object instance methods
User Reviews
Site Search
Recommended article
AD