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

[Lua][Help] Need help reading a file

Started by soccer16x, 08 February 2013 - 03:40 PM
soccer16x #1
Posted 08 February 2013 - 04:40 PM
So what I have being done is I'm writing to a file, then reading that and setting it so that what was in the file becomes a variable and then it sets the background color to what was in the file. But everytime I do it I get a Expected number error.
If you could help that'd be very nice.

Here's an example of the code I'm running:
h = fs.open("calcColor", "r")
borderColor = h.readLine()
h.close()
term.setBackgroundColor(borderColor)
And calcColor just reads colors.white
theoriginalbit #2
Posted 08 February 2013 - 04:42 PM
it gets read as a string from the file. so you need to do this.

borderColor = tonumber(h.readLine())


Wait just saw how you write it… gimme sec

borderColor = loadstring( "return "..h.readLine() )()
Edited on 08 February 2013 - 03:44 PM
soccer16x #3
Posted 08 February 2013 - 04:54 PM
Thanks works perfectly now
ChunLing #4
Posted 08 February 2013 - 07:37 PM
The first method would have worked and been a bit faster if you'd stored the tostring(colors.white) in the file rather than the string "colors.white".