4

I've started using Lua for school, and I'm wondering if it is possible to save a string to a txt file?

i.e

I have a string called shroom and it equals mush.

I have a file in the same folder as the .lua file called output.txt.

I now want to save the string shroom to output.txt.

I then want to make a while loop and every 1 second save the string shroom to the output.txt file, overwriting the contents of output.txt each time i save the string.

Is this possible in Lua? I've never used Lua before and I'm really confused at the moment..

Thanks :)

warspyking
  • 3,045
  • 4
  • 20
  • 37
Brandy
  • 544
  • 2
  • 11
  • 30
  • 1
    The first lesson about Lua should be: [Lua is not an acronym](http://stackoverflow.com/questions/13615975/what-does-lua-acronym-stand-for) :-) – Adam Jan 20 '16 at 22:34
  • @Adam yeah... See I thought it was `Lua` but my prof is some old dude who spells it as `LUA` so i just went with the old man's flow... – Brandy Jan 21 '16 at 01:12
  • 1
    @Brandon Well now you can school your prof. ;) – warspyking Jan 21 '16 at 01:25

1 Answers1

5

You can read this chapter from Programming in Lua to see how you can work with files; you'll need to use "w" mode when you open file and then call f:write(shroom) to do the write.

For sleeping in a loop you may want to check suggestions in this SO question.

Community
  • 1
  • 1
Paul Kulchenko
  • 25,884
  • 3
  • 38
  • 56