Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

appending text

Status
Not open for further replies.

mettodog

Vendor
Jul 11, 2000
94
US
how do i append the text in txtend.text to the end of a open text file?
 
Open the file in Append mode and then write out the text. This will append the text to end of the file.

Simon
 
OPEN <filename> for Append as #1

look up the Print # or &quot;Open statement&quot; in MSDN Index.
 
If the file is already open in some other mode, you will have to close it first before you open it in Append mode.

Simon
 
thanks, but i am still confused as how to actualy write in the appending mode, perhaps some sample code would help?...
 
Dim ffile as integer
ffile = freefile 'get a free file number
Open &quot;Textfile.txt&quot; for append as #ffile 'open file for append
print #ffile,txtend.text 'write the contents to the file
close #ffile 'close the file

David Paulson
 
Alternately, you can perform the same task in Binary mode.
I hope David doesn't mind if I use some of his code....
[tt]
Dim ffile as integer
ffile = freefile 'get a free file number
Open &quot;Textfile.txt&quot; for Binary as #ffile 'open file
Put #ffile, Lof(ffile)+1, txtend.text 'write the contents
close #ffile 'close the file
[/tt]

This just adds the contents of txtend.text to &quot;Textfile.txt&quot; starting at one byte after the end of the existing file: Lof(ffile)+1.
The advantage here is that you don't have to close the file and then re-open it in a different mode in order to perform additional manipulations. From this point, you can read the entire file into a text or list box, search for strings in the file, modify the contents and write back to the file without ever closing it.

A lot more versatile than simply allowing you to add a line to the end of the file.

In most cases binary file manipulation is the way to go. One reason is that it is several times faster. You don't need to read and write individual lines of data from a For/Next or Do Loop: you can read or write an entire file in one operation. Another reason is that it doesn't care what type of file you are working with (try reading Win386.swp after opening it For Input).
VCA.gif

Alt255@Vorpalcom.Intranets.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top