Hello Guest

append to file does not work

  • 3 Replies
  • 102 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

  • *****
  • 537
  • +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 »

*

Grantatted

  • *
  • 1
  • +0/-0
    • View Profile
    • Beauty salon
-
« Reply #2 on: November 25, 2020, 08:21:32 pm »
I would like to be able to open a file, write to it, close it and then reopen it to append more lines to it.
The write_file method appears to write to the start of the file. How can i make it append data to the end of the file?
Beauty salon in Kazan https://fwstyle.pro

*

Seuntjie

  • *****
  • 537
  • +14/-2
    • View Profile
Re: append to file does not work
« Reply #3 on: November 25, 2020, 09:12:38 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?