Creating new files with Lua I/O functions

这一生的挚爱 提交于 2019-12-23 10:56:45

问题


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!


回答1:


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().



来源:https://stackoverflow.com/questions/7277388/creating-new-files-with-lua-i-o-functions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!