Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Load a text file in one operation

Total Hit ( 2913)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


The fastest way to read a text file is using the Input$ function, as shown in this reusable procedure:

Click here to copy the following block
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

This method is much faster than reading each single line of the file using a Line Input statements. Here's how you can load a multiline textbox control with the contents of Autoexec.bat:

Click here to copy the following block
Text1.Text = FileText("c:\autoexec.bat")

UPDATE: Andrew Marshall wrote us to point out that the above routine fails when the file includes a Ctrl-Z (EOF) character, so we prepared a better version that works around that problem:

Click here to copy the following block
Function FileText(ByVal filename As String) As String
  Dim handle As Integer
  
  ' ensure that the file exists
  If Len(Dir$(filename)) = 0 Then
    Err.Raise 53  ' File not found
  End If
  
  ' open in binary mode
  handle = FreeFile
  Open filename$ For Binary As #handle
  ' read the string and close the file
  FileText = Space$(LOF(handle))
  Get #handle, , FileText
  Close #handle
End Function


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.