Creating and writing on a text file

Post Reply
Filion
Posts: 93
Joined: Sat Mar 26, 2011 4:21 pm

Creating and writing on a text file

Post by Filion »

So, i want to create a txt, that will keep tracks on the number of restats.
I have already catched the attribute points (using matches with i=2,3 etc)
I have failed to create the file, using the io library in general
So can somebody help me, with writing a basic code that:
a)creates/opens the file
b)writes a line on the bottom of the file.

For more advanced usage, i want to keep track and the number of the times that it has written the file (or something similar that will actually numerize the times that i have statted)

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: Creating and writing on a text file

Post by Heiko »

Check out the pinned Lua tutorial topic. It's fairly easy. http://forums.mudlet.org/viewtopic.php?f=9&t=641
especially this one: http://lua-users.org/wiki/IoLibraryTutorial
Note that you also have access to the Lua LFS library (Lua File System) as it's loaded by new versions of Mudlet.
http://keplerproject.github.com/luafilesystem/

Filion
Posts: 93
Joined: Sat Mar 26, 2011 4:21 pm

Re: Creating and writing on a text file

Post by Filion »

Thanks i ll try it, but where does lua create the file?

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: Creating and writing on a text file

Post by Heiko »

http://www.lua.org/pil/21.1.html

If you need an example look the unzip function in LuaGlobal.lua.

Filion
Posts: 93
Joined: Sat Mar 26, 2011 4:21 pm

Re: Creating and writing on a text file

Post by Filion »

so i made an alias to write this
Code: [show] | [select all] lua
f = io.open("statting.txt", "a")
f:write("hello world\n")
f:close()
but...nothing happens. I am not sure what to do, or actually, where to start from, i have tried to create by myself a txt file named statting, named statting.txt, inside the mudlet directory (is it really a concern) but still, nothing happened.

P.S. what unzip function in LuaGlobal.lua

Shingane
Posts: 3
Joined: Fri Oct 07, 2011 4:30 pm

Re: Creating and writing on a text file

Post by Shingane »

Maybe try doing f:flush() before closing the file?

Filion
Posts: 93
Joined: Sat Mar 26, 2011 4:21 pm

Re: Creating and writing on a text file

Post by Filion »

did, but didn't help

Edit, i found the txt file, it was on the mudlet dir, that for some reason is stored in a wierd place :D thanks for the help

Shingane
Posts: 3
Joined: Fri Oct 07, 2011 4:30 pm

Re: Creating and writing on a text file

Post by Shingane »

Is open() returning a valid file handle? Test if f is nil.

Filion
Posts: 93
Joined: Sat Mar 26, 2011 4:21 pm

Re: Creating and writing on a text file

Post by Filion »

Everything is working, thanks for the help

sophia57
Posts: 1
Joined: Tue Aug 20, 2024 9:03 am
Location: belgium
Contact:

Re: Creating and writing on a text file

Post by sophia57 »

Shingane wrote:
Mon Nov 07, 2011 7:55 pm
Is open() returning a valid file handle? Test if f is nil.
To ensure open() returns a valid file handle, check if f is nil. If f is nil, the file failed to open, and you should handle the error appropriately.

Post Reply