Brandy
Brandy

Reputation: 554

Lua save string to file

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 :)

Upvotes: 4

Views: 10668

Answers (1)

Paul Kulchenko
Paul Kulchenko

Reputation: 26744

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.

Upvotes: 5

Related Questions