Quote: "I needed to add text to my file, but there is no simple function for doing this"
Not in DBP's native commands but there is in the Matrix1 Utils. If you've not got this plugin yet, you really should. It's incredibly handy. You can find it
here. Below is your code adapted to use the plugin's datafile commands. As you can see, they are a lot simpler than the current method and avoid your carriage return issue.
`if file doesn't exist, create one
file$="txt.txt"
if file exist(file$)=0
open to write 1, file$
write string 1, "Hello?"
close file 1
endif
`Program start
AddLine( 1, file$, "Hi" )
end
`Program end
function AddLine( stream, file$, string$ )
open datafile to append stream, file$
write datafile string stream, string$
close datafile stream
endfunction
By the way, I think you had missed the 'e' off a couple of the file variables in your code here.
Anyhow back to the issue of why your code is having the extra carriage return problem, I've run your code and can confirm the issue. I believe that the write string command writes the string to the file and appends a carriage return to demarcate the different strings in a file. However it looks like the read string command may be reading the carriage return back into the string so when you use write string again, you now have two carriage returns instead of one. I've not been able to verify this as attempting simply to remove the last character of the string before writing it back does what you would expect (removes the 'i' from "Hi") and doesn't resolve the carriage return issue. However I did notice that when your variable is replaced with a string literal, the issue disappears so it is something to do with the variable.
Sorry I couldn't solve that for you (maybe someone else here has an idea of why it is happening?) but in reality, I think you are better off with the Matrix1 solution anyway.
Hope that helps
.