Easy Method of Writing And Reading Small Text Files
Posted
by Konstantin Komissarchik
on January 27th, 2004
In some specific cases, you can avoid the trouble by wrapping this functionality inside utility functions.
For example, 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"))

Comments
overwriting
Posted by steved3298 on 09/17/2004 09:35pmIs there any way to stop the program from writing to the first line and instead writing to the next line w/o text?
Replyhow to implement this code
Posted by Legacy on 06/14/2003 12:00amOriginally posted by: Danny Pryor
ReplyYour code returned false ; this code returns true
Posted by Legacy on 04/02/2003 12:00amOriginally posted by: Danny
ReplyFile write
Posted by Legacy on 03/02/2003 12:00amOriginally posted by: rahaman
ReplyReading and Writing text Files in VB
Posted by Legacy on 01/28/2002 12:00amOriginally posted by: Ignacio Diaz
Tried it and like it very good code. Made my life simpler.
ReplyThanks...
Flaw
Posted by Legacy on 03/21/2000 12:00amOriginally posted by: Aaron Wacker
If you read using input, then write using print, you are adding 2 characters to the file so it would be different than the original file. Try running the procedures recursively and you will see what I mean..
Reply