This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
oneupe1even's profile picture

[Lua] How do I read strings from another file?

Started by oneupe1even, 25 December 2012 - 06:21 AM
oneupe1even #1
Posted 25 December 2012 - 07:21 AM
So my partner in crime OmegaDrexus requested a program for our FTB company that would display some info about our workers. I got an idea where I would have two files, one file that would hold strings, and the other would actually print the strings on the other file. How would I do this?
remiX #2
Posted 25 December 2012 - 07:32 AM
Display info to your workers, how? On a monitor? Computer?
oneupe1even #3
Posted 25 December 2012 - 07:36 AM
I'm just printing it to the computer, I know how to print it to the monitor if I ever want to.
remiX #4
Posted 25 December 2012 - 07:46 AM
Then just print it to a computer? :P/>
print("text")

Unless you wanted to add text simply to a file for easy adding of text of something?
oneupe1even #5
Posted 25 December 2012 - 07:54 AM
Then just print it to a computer? :P/>
print("text")

Unless you wanted to add text simply to a file for easy adding of text of something?

Yeah, Omega is a code dummy. If I add the strings that hold the info to the file that prints it, I promise you he would screw it up. If I add the info to another file, than there would be less of a chance that he could mess it up. Example:


-- workerInfo file
local infoOmegaDrexus = [[Name: OmegaDrexus
Nickname: Omega
Job: Owner]]
remiX #6
Posted 25 December 2012 - 08:38 AM
This should be fine, it reads text from the "Omega/textInfo.txt" which is saved into the folder of the computer you're using.

Spoiler

s_mainDir = "Omega/"
s_infoFile = "infoText.txt"
s_fullPath = s_mainDir .. s_infoFile


if not fs.isDir(s_mainDir) then	fs.makeDir(s_mainDir) end
if not fs.exists(s_fullPath) then file = fs.open(s_fullPath, "w") file.close() end

function printText()
	t_info = {}
	i = 1
	fileHandle = fs.open(s_fullPath, "r")
	if fileHandle then
		line = fileHandle.readLine()
		repeat
			table.insert(t_info, line)
			line = fileHandle.readLine()
		until line == nil
	end
	fileHandle.close()
	for i = 1, #t_info do
		print(t_info[i])
	end
end

function addText(text)
	fileHandle = fs.open(s_fullPath, "a")
	if fileHandle then
		fileHandle.writeLine(text)
	end
	fileHandle.close()
end

while true do
	term.clear()
	term.setCursorPos(1,1)
	printText()
	repeat
		e = {os.pullEvent()}
	until string.lower(e[2]) == "r"
end
oneupe1even #7
Posted 25 December 2012 - 09:09 AM
I saw someone else do it with io.open, which is kind of how I wanted to do it. How would I do what I wanted to do with io.open?
remiX #8
Posted 25 December 2012 - 09:15 AM
If you're just using it to read text, then there's really no difference :P/>
oneupe1even #9
Posted 25 December 2012 - 09:42 AM
io.open sounds a little bit easier to understand.