f:write doesn't want to write

Posted by Ogomez92 on Tue 01 Dec 2009 03:32 PM — 11 posts, 48,353 views.

#0
Hi,
I've been trying to get thi to work for over 2 hours now and I just don't seem to get the point.
I'mt rying to get a script to write soemthing to a file, like this:
f=io.input(GetInfo(67).."/db.txt")
dbw=f:write("hey\n")
Note(dbw)
This note returns nil
and then the file is the same as it was
I want to append hey to the end of that file because there's already something written in it.
Help would be appreciated. Thanks.
USA #1
I'm not an expert, but it could be that it's because you opened the file for /input/ which pretty much makes it read-only not output. You might want to use io.open() instead.

Edit: From the help file, I'm guessing you want either mode "a" or "a+" for appending.
Amended on Tue 01 Dec 2009 05:46 PM by Cage_fire_2000
#2
Oh, my bad! Thanks for that. I was opening it as input so I couldn't write to it. Doh!
#3
Hi, sorry peeps but I'm having another frustrating issue.
I got it to write down the value of a variable just fine, but no matter what I do, I can't seem to get it to write a newline. I tried concatinating a newline to the variable like this: f:write(GetVariable("var").."\n") and it tells me unfinished string. I also tried concatinating it directly after the variable outside the write function and still, unfinished string. I even tried doing the var first which works fine and then in another function f:write. Btw, all these bracesare ment to be backslashes, dunno why it doesn't let me typethem on this site. I'm pasting the trigger hereb ecause I don't know what's wrong. The thing is that it should read a file, see if the character who jsut logged on is recognized, otherwise configure its prompts etc.

dbplayers={}
db=io.input(GetInfo(67).."/db.txt")
line=db:read("*l") -- read one line
while line do -- if not end of file (EOF)
table.insert(dbplayers,line)
line = db:read ("*l") -- read one line
end
db:close()
me=GetVariable("me")
if (IsInTable(dbplayers,GetVariable("me"))~=1) then
dbw=io.open(GetInfo(67).."/db.txt","a+")
dbw:write(GetVariable("me").."\n")
dbw:close()
Note("config mud")
else
Note("recognized.")
end
USA #4
Hmm, I'm not sure, I've never used the file io routines in Lua, I suppose you could try changing "\n" to "\r\n", that's the DOS code for a new line, I suppose "\n" might not be registering as a newline, although I'm not sure if that matters. I know from recent experience on my system I need to use "\r\n" when appending to a notepad window.

I'm not sure what you're trying to do. Are you keeping some sort of global list of player names for the different worlds?
Amended on Tue 01 Dec 2009 08:05 PM by Cage_fire_2000
#5
Hmm nope. Sadly, \r\n does not work either.
dbw:write(GetVariable("var"),"\r\n")
Compile error
Plugin: AlterAeon (called from world: input)
Immediate execution
[string "Trigger: "]:18: unfinished string near '"'
Btw jus wondering why does this forum convert all the backslashes to \? At least it does in the box where you write...
USA #6
If you have forum codes enabled, it converts any \ that's not a valid forum code into \\. (Manually typing those so that you see what I mean, I have to double the backslashes up.)

Triggers and other things with send boxes don't do well with backslashes. The reason is that MUSHclient parses the content once before the scripting engine gets to it. So, \\\\" becomes \\" when MUSHclient begins parsing it, which then becomes \" when Lua parses it. So... try using four slashes instead of one when you're editing a trigger.
Amended on Tue 01 Dec 2009 08:18 PM by Twisol
USA #7
Ogomez92 said:

Btw jus wondering why does this forum convert all the backslashes to \? At least it does in the box where you write...

Isn't a backslash supposed to be \ ? Maybe you have strange font settings or something, but I see your backslashes coming out as backslashes...

But, this does make me wonder if we're seeing what we're supposed to be seeing here. Could you try posting your code to pastebin.com to make sure we're all on the exact same page? If Lua thinks that the string is unfinished, it could very well be because of a missed escape or something like that.

Also, where exactly are you entering all this? (in MC that is)



EDIT: Ah, listen to Twisol. :-)
Amended on Tue 01 Dec 2009 08:19 PM by David Haley
#8
Does that mean that I should make it f:write(whatever.."\\n")?
Amended on Tue 01 Dec 2009 11:43 PM by Nick Gammon
USA #9
Yes. I think you should also disable forum codes in your posts, because - as I said - the four slashes you posted get squished down to two, so we see two when you wrote four.

I.e. what we see in that post is wrong (two slashes), but what I'm sure you intended is correct (four slashes).
Australia Forum Administrator #10
In the "send" box, you need to use \\r\\n to get a carriage-return linefeed (because MUSHclient changes \\ to \ and then Lua changes \r to carriage-return and \n to linefeed).

In a script file you don't need to do that because there is no preprocessing (in other words just use \r\n).

As for appending to a file, see:

http://www.gammon.com.au/scripts/doc.php?lua=io.open

I would be opening with mode "a" (append to existing).