Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating new files with Lua I/O functions

Tags:

io

text-files

lua

I'm starting to learn about the io. functions, and am trying to implement them in my code. I've searched for the answer to this and nothing seems to give a clear cut yes or no, or at least I don't see one. I'm hoping someone here will know the answer and be able to help with this.

I'm wanting to create a text file that I can write to as time progresses. It'll basically be a log to which I'll be appending lines of output. Apparently io.open("textfile.txt") does not create the file, or so it appears.

Is there a way to create a text file in Lua that can later be accessed with io.read/write? Additionally, do I need to call io.close() before opening or creating a new text file? I appreciate any help given. Thanks!

like image 622
Josh Avatar asked Oct 10 '22 02:10

Josh


1 Answers

You need to open the file for writing as follows: f=io.open("textfile.txt","w"). Then use f:write() to write stuff to it. When finished writing, call f:close().

like image 91
lhf Avatar answered Oct 14 '22 02:10

lhf