Text to textfile.

Post Reply
Zenith
Posts: 11
Joined: Sat Jul 09, 2011 7:29 am

Text to textfile.

Post by Zenith »

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.

Jor'Mox
Posts: 1146
Joined: Wed Apr 03, 2013 2:19 am

Re: Text to textfile.

Post by Jor'Mox »

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.

Jor'Mox
Posts: 1146
Joined: Wed Apr 03, 2013 2:19 am

Re: Text to textfile.

Post by Jor'Mox »

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.
Code: [show] | [select all] lua
-- File IO Script
-- By: Zachary Hiland
-- 1/1/2014

fileIO = fileIO or {}
fileIO.__index = fileIO

function fileIO.fileDialog(title, target)
	if target == "folder" then target = false else target = true end
	local path = invokeFileDialog(target, title)
	return path
end

function fileIO.exists(filename)
	if type(filename) == "table" and filename.type == "fileIO_file" then filename = filename.name end
	local info = lfs.attributes(filename)
	if info.mode == "file" then
		return true
	else
		return false
	end
end

function fileIO.open(filename,mode)
	local errors
	mode = mode or "read"
	assert(table.contains({"read","write","append","modify"},mode),"Invalid mode: must be 'read', 'write', 'append', or 'modify'.")
	if mode ~= "write" then
		local info = lfs.attributes(filename)
		if not info then
			errors = "Invalid filename: no such file."
			return nil, errors
		end
		if info.mode ~= "file" then
			errors = "Invalid filename: path points to a directory."
			return nil, errors
		end
	end
	local file = {}
	file.name = filename
	file.mode = mode
	file.type = "fileIO_file"
	file.contents = {}
	if file.mode == "read" or file.mode == "modify" then
		local tmp = io.open(file.name,"r")
		local linenum = 1
		for line in tmp:lines() do
			file.contents[linenum] = line
			linenum = linenum + 1
		end
		tmp:close()
	end
	setmetatable(file,dslpnp.fileIO)
	return file, nil
end

function fileIO.read(file, line)
	assert(file.type == "fileIO_file", "Invalid file: must be file returned by fileIO.open.")
	assert(line == "all" or tonumber(line), "Invalid argument: must be 'all' or the line number to be read.")
	local text = ""
	if line == "all" then
		for k, v in ipairs(file.contents) do
			text = text .. v .. "\n"
		end
	else
		text = file.contents[line]
	end
	return text
end

function fileIO.lines(file)
	local line = 0
	return function ()
		line = line + 1
		if file.contents[line] then
			return line, file.contents[line]
		else
			return nil
		end
	end
end

function fileIO.write(file, text, line)
	assert(file.type == "fileIO_file", "Invalid file: must be file returned by fileIO.open.")
	assert(line == nil or tonumber(line), "Invalid argument: must be the line number to write to or nil (to write at the end).")
	text = tostring(text)
	if line then
		file.contents[line] = text
	else
		table.insert(file.contents,text)
	end
	return file
end

function fileIO.delete(file, line)
	assert(file.type == "fileIO_file", "Invalid file: must be file returned by fileIO.open.")
	assert(line == "all" or tonumber(line), "Invalid argument: must be 'all' or the line number to be deleted.")
	if line == "all" then
		file.contents = {}
	else
		table.remove(file.contents,line)
	end
	return file
end

function fileIO.close(file)
	assert(file.type == "fileIO_file", "Invalid file: must be file returned by fileIO.open.")
	local tmp
	if file.mode == "write" then
		tmp = io.open(file.name,"w")
	elseif file.mode == "append" then
		tmp = io.open(file.name,"a")
	elseif file.mode == "modify" then
		tmp = io.open(file.name,"w+")
	end
	if tmp then
		for k,v in ipairs(file.contents) do
			tmp:write(v .. "\n")
		end
		tmp:flush()
		tmp:close()
		tmp = nil
	end
	return true
end

User avatar
Vadi
Posts: 5042
Joined: Sat Mar 14, 2009 3:13 pm

Re: Text to textfile.

Post by Vadi »

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.

mark
Posts: 1
Joined: Fri Aug 12, 2016 2:36 pm

Re: Text to textfile.

Post by mark »

@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?

EulersIdentity
Posts: 27
Joined: Fri Jun 26, 2015 8:52 am

Re: Text to textfile.

Post by EulersIdentity »

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:
Code: [show] | [select all] lua
-- The file you're editing is going to be assigned a name. We can open the text file "my_stats.txt". Internally, this will be called "stats_file".
stats_file = io.open("my_stats.txt", "a")  -- the "a" means append to end of file

-- set the file to be the default for outputting
io.output(stats_file)

-- output the contents of some variables to the file
io.write("Str:"..my_str.." Int:"..my_int.." Wil:"..my_will.." Dex:"..my_dex.." Con:"..my_con.."\n")

-- close the file
io.close(stats_file)
Sorry if anything doesn't work exactly right, it's been a minute since I've written file io code.

Post Reply