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!

URGENT-saving sting to text file 1

Status
Not open for further replies.

Craftor

Programmer
Feb 1, 2001
420
NZ
Hello again all

I know there is a certain syntax to use when you want to copy a string variable to a file - something along the lines of

dim int as integer
int = freefile
'all the rest of the code

Could someone please give me the full syntax to copy the contents of a string variable to a text file??

In case anyone is wondering, it HAS to be a text file as this file is opened in Word and printed out.

Thanks, all

:)
 
Tried that - got an error on the last line of code (above) to say object doesn't support this property or method
 
See faq222-482 "A faster way to load a text file into an array". This is a small discussion regarding the advantages of using the Binary disk method as opposed to the standard Input, Output and Append disk methods. This is much faster and actually easier to use than the standard methods. Say you wanted to add a line of text to a file, you could use Mick's suggestion and: [tt]

MyString$ = "A new line of text."
ff = Freefile
Open "BlaBla.txt" For Append as #ff
Print #ff, MyString$
Close #ff
[/tt]

In order to retrieve the last line you would have to open the file for Input and read each line, one by one, until you reached the end of the file:[tt]
ff = Freefile
Open "BlaBla.txt" For Input as #ff
Do While Not EOF(ff)
Input #ff, MyString$
Loop
Close #ff
MsgBox MyString$
[/tt]

This is highly inefficient, especially if the text file is large. A quicker way is to use the Binary method.
Write a line at the end of the file....
[tt]
MyString$ = String$(30,32)
LSet Mystring$ = "A new line of text."
ff = Freefile
Open "BlaBla.txt" For Binary as #ff
Put #ff, LOF(ff) + 1, MyString$
Close #ff
[/tt]

Read the last line from the file...[tt]
MyString$ = String$(30,32)
ff = Freefile
Open "BlaBla.txt" For Binary as #ff
Get #ff, LOF(ff) - Len(MyString$), MyString$
Close #ff
MsgBox Trim$(MyString$)[/tt]


This way we can go directly to the last line of the file instead of having to read every line that preceeds it.

Even better is the Random access method. The record structure has to be pre-defined but it is very easy to read or write any given record in a file without messing with the other records.[tt]

'Assume a maximum size of each line as 255 characters
'(Why? I just like that number.)
MyString$ = String$(255,32)
ff = Freefile
Open "BlaBla.txt" For Random as #ff LEN=Len(MyString$)
'Get the 30th record...
Get #ff, 30, MyString$
MsgBox Trim$(MyString$)
'Get the last record, whatever it might be...
Get #ff, Lof(ff) \ Len(MyString$), MyString$
MsgBox Trim$(MyString$)
'Write to the 5598th record...
LSet Mystring$ = "I wrote this."
Put #ff, 5598, MyString$
'Get the 1st record...
Get #ff, 1, MyString$
MsgBox Trim$(MyString$)
'Get the 5598th record...
Get #ff, 5598, MyString$
MsgBox Trim$(MyString$)
Close #ff
[/tt]

Of course you would have to include error checking to make sure there are actually 5598 records in the file... but this method is much faster and adaptable for reading and writing to a file than the Input# and Print# statements could ever allow.

Lets say you want to change a period to an exclamation mark in the 5598th line in a file. Imagine opening a 32000 line text file, changing the 5598th line and then saving the file again. You would have to open the file for Input, create a temporary file and open it for Output, read 5597 lines from the text file and write them to the temporary file, read the 5598th line and change the string, write it to the temp file, read the remaining lines from the text file and append them to the temp file, close both files, copy the temp file to the text file and delete the temp file.

I would prefer:
[tt]
Get #ff, 5598, MyString$
Mid$(MyString$,13,1) + "!"
Put #ff, 5598, MyString$
[/tt]


It's much faster and less prone to coding errors! LOL

VCA.gif

Alt255@Vorpalcom.Intranets.com
 
Sorry. Talk about coding errors. In the 2nd from the last line of code you might try:

[tt]Mid$(MyString$,13,1) = "!"[/tt]

It seems to equate better.

VCA.gif

Alt255@Vorpalcom.Intranets.com
 
Thanks a mil Alt225 - am gonna try that this weekend - it looks like it is EXACTLY what I am looking for!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top