Advertisement
4_2005-2006 Files/ File Controls/ Input/ Output #159523

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.

AI

Tóm tắt bởi AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Mã nguồn
original-source
'-- If you have any problems with this code please contact me
'-- at [email protected]. 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
Bình luận gốc (3)
Được khôi phục từ Wayback Machine