Search Tools Links Login

Functions For Reading and Writing Text Files


This code consists of 2 Function (ReadFile and WriteFile). All you have to do is Point ReadFile to a filepath and it will return the text within that file. Write file recieves a filepath and the string value to save to it. They include simple error handling..and can easily and quickly be advanced to handle open and save dialogs.

Original Author: Patrick Daniel

Code

'-- If you have any problems with this code please contact me
'-- at patrick1@mediaone.net. Feel free to drop me a line
'-- letting me know you are using this or if this code is
'-- helpfull to you. Enjoy!!
Public Function ReadFile(strPath As String) As Variant
On Error GoTo eHandler
  
  Dim iFileNumber As Integer
  Dim blnOpen As Boolean
  
  iFileNumber = FreeFile
  
  Open strPath For Input As #iFileNumber
  
  blnOpen = True
  
  ReadFile = Input(LOF(iFileNumber), iFileNumber)
  
eHandler:
  
  If blnOpen Then Close #iFileNumber
  
  If Err Then MsgBox Err.Description, vbOKOnly + vbExclamation, Err.Number & " - " & Err.Source
  
End Function
Public Function WriteFile(strPath As String, strValue As String) As Boolean
On Error GoTo eHandler
  Dim iFileNumber As Integer
  Dim blnOpen As Boolean
  
  iFileNumber = FreeFile
  
  Open strPath For Output As #iFileNumber
  
  blnOpen = True
  
  Print #iFileNumber, strValue
  
eHandler:
  
  If blnOpen Then Close #iFileNumber
  
  If Err Then
   MsgBox Err.Description, vbOKOnly + vbExclamation, Err.Number & " - " & Err.Source
  Else
   WriteFile = True
  End If
  
End Function

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 92 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.