io.open error

Posted by Rhien on Wed 20 Dec 2017 03:31 AM — 4 posts, 16,550 views.

#0
I'm digging MushClient after a week.

I'm writing some Lua to log data out to a flat file and I'm running into an issue. I tried this code in a Lua IDE locally and it worked, in MushClient I get an error. In the IDE I set trigger text to something random.

The trigger returns %0 correctly as I want, the io.open tosses an error though which is also below.


local triggerText = "%0"
file = assert(io.open("C:\\Temp\\test.txt", "a"))
io.output(file)
io.write(os.date("%m/%d/%Y "))
io.write(triggerText)
io.close(file)



Run-time error
World: Mud
Immediate execution
[string "Trigger: "]:2: C:Temp  est.txt: Invalid argument
stack traceback:
        [C]: in function 'assert'
        [string "Trigger: "]:2: in main chunk


Although not mentioned in the error, I have trusted all world in the global settings I have permissions to the folder in question. I've also tried the other append/write modes.

Any thoughts on what I'm missing?

Edit, but will leave for posterity -->

Got it working with this... The odd formatting of the file in the error seemed like it wasn't reading the escapes right.. went from 2 per slash to 4.. and it worked. I'm not sure why it needs 4, but it worked after.


file = assert(io.open("C:\\\\Temp\\\\test.txt", "a"))
Amended on Wed 20 Dec 2017 03:43 AM by Rhien
Australia Forum Administrator #1
I was about to reply, but you worked it out. There are two levels of escaping of backslashes at work here. First the "send" box in a trigger lets you use backslashes so you have to escape them. Second, in Lua, you need to escape backslashes in a string literal. The upshot is that you need 4 backslashes to get one inside a string literal.

If you had used a script file (not "send to script") then the Lua code would have worked in the expected way (you only need 2 backslashes).
Australia Forum Administrator #2
I think you can get away with forward slashes in pathnames, which makes this particular problem go away.
#3
Thank you for the explanation Nick, that makes perfect sense.