[Closed] Simple filestream script crashes max
Hi,
Could someone explain to me why this is crashing max every time ?
I’m losing sanity.
(
file = "C:/a.txt"
fs = openfile file mode:"r+" encoding:#utf8
a = readLine fs
a += "tata"
flush fs
close fs
)
a.txt contains whatever string like toto
It crashes when i add the string “tata” to string a
If i do :
b = copy a
–in listener “toto”
b += “tata”
i get 翼tata
wtf
I thought it had something to do with the text file encoding, but i’m stuck
Thanks !
Looks like it has more to do with the mode, because mode:“rt” is working.
I guess i could close the file after reading it and reopen it or recreate it to write stuff.
Still how is that read/write mode meant to work ? Bug ? Feature ? Who knows…
try to read the file correct and safe:
(
file = @"с:\a.txt"
if doesfileexist file and (fs = openfile file) != undefined do
(
while not eof fs do
(
str = readLine fs
str += "tata"
)
close fs
)
)
Hey, thanks. Yeah that’s what i did in the end.
I only wanted to add some values to a .csv each time i run the script, but now i recreate the file each time.
I open first in default read text mode (rt), read, close.
Then open in write text mode (wt), write, close.
There’s something i don’t get with the r+ read/write text mode. Maybe i’m overwriting the initial memory address, but i shouldn’t if i use copy ?
If you just need to append some text to a file, I would use “a+” mode:
for j = 1 to 256 do
(
stream = openfile @"C:\test.csv" mode:"a+" encoding:#utf8
format "% - %\n" j (bit.intaschar j) to:stream
flush stream
close stream
)