I do not understand TextOutputStream.Append

Hi,

I wanted yesterday to save data into a text file from a loop, one line at a time, using TextOutputStream.Append.

At first, I totally does not understand how to use it. My first attempt does not works at all.

Here’s an idea of the used code

Dim FolderItem and TextOutputStream

Create instances of FolderItem and TextOutputStream

In a loop:
a. Read one line from the source text file
b. Do some trickery to that line (and store it in OneLine)
c. Append OneLine in target text file

I had constant crash at the .Append line. The file was created, but empty at crash time.
The target text file was filed with the first line correctly modified when the IDE application quits.

Then, I checked:
http://documentation.xojo.com/index.php/TextOutputStream.Append

I removed the Dim / Create instance for the target text file,
copy / paste the example from docs,
and it worked.

Then, I commented out that code, try back my own and found it working reliabily. *
Why ?

That line in a loop looks to me like no-no. I cannot understand that line executed, say 100, 200 , 10,000 times …

Dim t as TextOutputStream = TextOutputStream.Append(f)

What is your feeling ?

  • It happens to me too using AppleScript (with the Script Editor): the script worked fine, but a copy/paste was wrong (and it still works !) Incredible.

Have you read this part of http://documentation.xojo.com/index.php/TextOutputStream

Creating and Appending to a Text File
Use Append when you want to open an existing text file and append text data to it. Use Create to write to a new text file. The following two examples illustrate the difference. Each example writes the text in TextField1 to the text file.

Hi John,

yes I have.

What I wanted to do was to write to a brand new text. I had hard time 'till I copy / paste the example from the page. (in fact the relevant line to append / with .Append).
The strange thing is that my prior code was working after that, when I checked back (uncomment old code / comment new code).

OK, forget it. I certainly was in bad mood and it worked yesterday by pure hazard. (I do not have the project with me.)

Hello Emile,

You use Textoutputstream.append to create a new file if it is not already created and also to open that file to append text.

You do that before the loop.

In the loop you use Textoutputstream to do the actual appending with writeline

TOS = Textoutputstream.writeline sometext // in the loop some text is a string

Lennox

Thank you.