Hello Guest

append to file does not work

  • 1 Replies
  • 34 Views
*

vineeee

  • **
  • 15
  • +0/-0
    • View Profile
append to file does not work
« on: October 17, 2020, 08:44:47 pm »
Hello all,
i need append mode in file open.
I google for about 30 hours, i tray all, i read all documentation of lua 5.1 and also new one 5.5+, i also read stack overflow...

Example:

That works;
function zapisidatoteko()
local file = io.open("test.txt", "w")
file:write(zapiski)
file:close()
end

That works;
function prebere()
local file = io.open("1.txt", "r")
file:seek("end",-25)
print(file:read("*a"))
-- closes the open file
file:close()
end

That does not work, tray all combinations, old lua syntaks new lua, same, does not work..

-- Opens a file in append mode
file = io.open("test.lua", "a")

-- appends a word test to the last line of the file
file:write("--test")

-- closes the open file
file:close()



Got error:
[string "chunk"]:37: attempt to index local 'file' (a nil value)


Please help me. I am desperate. Thank you!

*

Seuntjie

  • *****
  • 516
  • +14/-2
    • View Profile
Re: append to file does not work
« Reply #1 on: October 17, 2020, 11:15:53 pm »
While not idea, the solution provided on this post works: https://forum.seuntjie.com/index.php?topic=331.msg622#msg622

Edit: I have to ask, why do you want to write out to a file? All bets are logged to a sqlite database called dicebot.db, that you can open and query using something like sqlitebrowser. Can you not get your answer from that instead?
« Last Edit: October 17, 2020, 11:17:43 pm by Seuntjie »