If i want to save a line of text to a text file, C:\Users\Zenith\AppData\Local\Mudlet\Mudlet Stats.txt how would i do that?
The line of text i want to save everytime it appears is:
Your base abilities are: Str:xx Int:xx Wil:xx Dex:xx Con:xx.
Too: C:\Users\Zenith\AppData\Local\Mudlet\Mudlet Stats.txt
And is it possible to get it listed like this?
Your base abilities are: Str:15 Int:14 Wil:19 Dex:16 Con:10.
Your base abilities are: Str:15 Int:14 Wil:19 Dex:14 Con:10.
Your base abilities are: Str:15 Int:15 Wil:15 Dex:12 Con:13.
Your base abilities are: Str:12 Int:18 Wil:19 Dex:14 Con:10.
Your base abilities are: Str:12 Int:16 Wil:15 Dex:14 Con:13.
Your base abilities are: Str:12 Int:15 Wil:19 Dex:14 Con:14.
Your base abilities are: Str:15 Int:16 Wil:19 Dex:12 Con:10.
Your base abilities are: Str:16 Int:16 Wil:19 Dex:14 Con:9.
Your base abilities are: Str:15 Int:18 Wil:15 Dex:14 Con:10.
Your base abilities are: Str:16 Int:15 Wil:19 Dex:14 Con:12.
Your base abilities are: Str:12 Int:16 Wil:15 Dex:17 Con:14.
Text to textfile.
Re: Text to textfile.
I have a script written that makes file reading and writing a bit less tedious (almost easy I think). I'll dig it up and post it once I get to my computer.
Re: Text to textfile.
This should give you all the tools you need to read and write to files. You can use fileDialog to bring up a dialog window to select a file if necessary, check if a file exists, and then open a file (in "read", "write", "append", or "modify" mode). Once it is open, the returned file object can use the following methods:
read(line) -- returns a line of text specified by line, or "all" to return all text as a single string
write(text, line) -- writes new text to the file, either replacing the line specified by line, or appending it to the end of the file if line is nil
lines() -- creates an iterator for a for loop that will return the line number and the contents of each line
delete(line) -- deletes a particular line from a file, or "all" to clear all text
close() -- closes the file and writes all changes, in a manner appropriate to the mode specified when the file was opened.
read(line) -- returns a line of text specified by line, or "all" to return all text as a single string
write(text, line) -- writes new text to the file, either replacing the line specified by line, or appending it to the end of the file if line is nil
lines() -- creates an iterator for a for loop that will return the line number and the contents of each line
delete(line) -- deletes a particular line from a file, or "all" to clear all text
close() -- closes the file and writes all changes, in a manner appropriate to the mode specified when the file was opened.
Re: Text to textfile.
Please don't save anything to that location. It is for the Mudlets installation, not for your use, and very well could be deleted when you uninstall or upgrade Mudlet. Take a look at getMudletHomeDir() for getting a good location.
Re: Text to textfile.
@Jor'Mox This looks pretty helpful, but I'm not sure how to actually use it. Is this code supposed to be copied into a script, or imported somehow? Can you provide an example of how to install and then use the functions please?
-
- Posts: 27
- Joined: Fri Jun 26, 2015 8:52 am
Re: Text to textfile.
It looks like he just added some error checking to the normal file IO functions for Lua. They should behave similarly to the standard io functions. There are tutorials on the io library -Here- and -Here-. To use his script, just copy-paste the whole thing into an empty script in Mudlet and you should be good to go.
Quick overview: Lua works similarly to other languages in dealing with file I/O. Any file you wish to read from or write to must first be opened. Then you can edit or read the file, then it must be closed. It's important to be sure to close a file so that there is no accidental data corruption with the file if something happens, ie: Mudlet crashes with the file open.
Example:
Quick overview: Lua works similarly to other languages in dealing with file I/O. Any file you wish to read from or write to must first be opened. Then you can edit or read the file, then it must be closed. It's important to be sure to close a file so that there is no accidental data corruption with the file if something happens, ie: Mudlet crashes with the file open.
Example:
Sorry if anything doesn't work exactly right, it's been a minute since I've written file io code.