VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Easy method of Writing And Reading Small Text File

by Mike-Ejeet 9t9 (5 Submissions)
Category: Miscellaneous
Compatability: Visual Basic 3.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (2 Votes)

Easy method of Writing And Reading Small Text Files

Side Effects
In a production application, every time you want to access a file for reading or writing, you have to retrieve a free handle using the FreeFile() function to make sure that you do not overwrite an existing handle. Not only this, but you have to remember to close the file after you are done with it.

Rate Easy method of Writing And Reading Small Text File

I wrote the following two functions to go between strings and text files in my apps:
public Function ReadFile(FileName as string) as string
  Dim i as Integer
  i = FreeFile
  on error GoTo ErrorTrap
  Open FileName for input as #i
  ReadFile = input(LOF(i), i)
  Close #i
  Exit Function
ErrorTrap:
  ReadFile = ""
End Function

public Sub WriteFile(FileName as string, Contents as string)
  Dim i as Integer
  i = FreeFile
  Open FileName for Output as #i
  print #i, Contents
  Close #i
End Sub
***Once these functions are in your project, you have a quick way of reading and writing text files. For example, the following code is a weird way of copying text files: 

Call WriteFile("c:\b.txt", ReadFile("c:\a.txt"))



Download this snippet    Add to My Saved Code

Easy method of Writing And Reading Small Text File Comments

No comments have been posted about Easy method of Writing And Reading Small Text File. Why not be the first to post a comment about Easy method of Writing And Reading Small Text File.

Post your comment

Subject:
Message:
0/1000 characters