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

read a color out of a file

Started by Leon669, 13 April 2015 - 03:25 PM
Leon669 #1
Posted 13 April 2015 - 05:25 PM
Hi

i have a file only the color red in it

FileColor

red

so now in another program i want to add this color like

fg = fs.open("loginSettins/version","r")
farbe = fg.readLine()
paintutils.drawPixel(1,5,colors.farbe)
fg.close()

it doesnt work what to do ?
Edited on 13 April 2015 - 05:23 PM
Lyqyd #2
Posted 13 April 2015 - 05:37 PM
You can't do that at all, you have to read through the contents of the file in sequence, and you only get to do so one time through. I believe the readLine() method works on the handles returned by http.get as well.
flaghacker #3
Posted 13 April 2015 - 05:42 PM
Readline reads the next line, so calling readline multiple times will read diffrent lines.


file = fs.open ("aFile", "r")

print (file.readLine ()) --first line
print (file.readLine ()) -- second line
print (file.readLine ()) --you get the pattern...
...
file.close ()

You can also use file.readAll () and split the text using string.split, I'm notgoing to put an example here becauze I'm on mobile right now.
Leon669 #4
Posted 13 April 2015 - 05:45 PM
Readline reads the next line, so calling readline multiple times will read diffrent lines.


file = fs.open ("aFile", "r")

print (file.readLine ()) --first line
print (file.readLine ()) -- second line
print (file.readLine ()) --you get the pattern...
...
file.close ()

You can also use file.readAll () and split the text using string.split, I'm notgoing to put an example here becauze I'm on mobile right now.

thx it work ;)/>

but how to read pastebin files ?
KingofGamesYami #5
Posted 13 April 2015 - 06:22 PM

local file = http.get( "URL_TO_RAW_PASTE_DATA" )
local data = file.readAll()
print( data )
Anavrins #6
Posted 13 April 2015 - 06:28 PM
You can get in the whole file into a table like this

local file = http.get("www.example.com")
local content = {}
for line in file.readLine do
  table.insert(content, line)
end
And get specific lines such as line 3 with print(content[3])
To read from pastebin, you would use it's raw link "http://pastebin.com/raw.php?i=pAsTeC0dE"
Edited on 13 April 2015 - 04:29 PM
Anavrins #7
Posted 13 April 2015 - 07:51 PM
Could you not have made another topic altogether instead?

Anyways, colors.farbe does not exist in the colors api.
And since farbe is a string, you need to convert it into a number.
So paintutils.drawPixel(1,5, tonumber(farbe)) would be the way to go.
Edited on 13 April 2015 - 05:54 PM
Leon669 #8
Posted 13 April 2015 - 08:19 PM
Could you not have made another topic altogether instead?

Anyways, colors.farbe does not exist in the colors api.
And since farbe is a string, you need to convert it into a number.
So paintutils.drawPixel(1,5, tonumber(farbe)) would be the way to go.

doenst work it always shows lightBlue

Code
Spoiler


shell.run"clear"
——————————————————
Bild = paintutils.loadImage("OS/d1.img")
paintutils.drawImage(Bild,1,1)
——————————————————
term.setBackgroundColor(colors.lightGray)
term.setTextColor(colors.white)
—–
fg = fs.open("loginSettins/version","r")
farbe = fg.readLine()
——————————————————
paintutils.drawPixel(49,17, colors.blue)
paintutils.drawPixel(50,18, colors.blue)
paintutils.drawPixel(50,17, colors.blue)
paintutils.drawPixel(49,18, colors.blue)
paintutils.drawPixel(48,17, colors.blue)
paintutils.drawPixel(48,18, colors.blue)






term.setTextColor(colors.gray)
term.setBackgroundColor(colors.white)
term.setCursorPos(1,7)
print("Settings")
term.setCursorPos(2,12)
print(" News")
term.setCursorPos(2,17)
print(" Worm")
term.setCursorPos(11,7)
print("ChromeMenu")
term.setCursorPos(13,12)
print("NPaint")
term.setCursorPos(13,17)
print("XFiles")
term.setTextColor(colors.white)
term.setBackgroundColor(colors.blue)
term.setCursorPos(48,17)
print("")
term.setCursorPos(48,18)



paintutils.drawPixel(3,4,40000)
paintutils.drawPixel(4,4,40000)
paintutils.drawPixel(5,4,40000)
paintutils.drawPixel(6,4,40000)
paintutils.drawPixel(3,5,40000)
paintutils.drawPixel(6,5,40000)
paintutils.drawPixel(3,6,40000)
paintutils.drawPixel(4,6,40000)
paintutils.drawPixel(5,6,40000)
paintutils.drawPixel(6,6,40000)
————
paintutils.drawPixel(3,9,3000)
paintutils.drawPixel(4,9,3000)
paintutils.drawPixel(5,9,3000)
paintutils.drawPixel(6,9,3000)
paintutils.drawPixel(3,10,3000)
paintutils.drawPixel(6,10,3000)
paintutils.drawPixel(3,11,3000)
paintutils.drawPixel(4,11,3000)
paintutils.drawPixel(5,11,3000)
paintutils.drawPixel(6,11,3000)
—————
paintutils.drawPixel(3,14,20000)
paintutils.drawPixel(4,14,20000)
paintutils.drawPixel(5,14,20000)
paintutils.drawPixel(6,14,20000)
paintutils.drawPixel(3,15,20000)
paintutils.drawPixel(6,15,20000)
paintutils.drawPixel(3,16,20000)
paintutils.drawPixel(4,16,20000)
paintutils.drawPixel(5,16,20000)
paintutils.drawPixel(6,16,20000)
————–
paintutils.drawPixel(14,4,33)
paintutils.drawPixel(15,4,33)
paintutils.drawPixel(16,4,33)
paintutils.drawPixel(17,4,33)
paintutils.drawPixel(14,5,33)
paintutils.drawPixel(17,5,33)
paintutils.drawPixel(14,6,33)
paintutils.drawPixel(15,6,33)
paintutils.drawPixel(16,6,33)
paintutils.drawPixel(17,6,33)
——————-
paintutils.drawPixel(14,9,17)
paintutils.drawPixel(15,9,17)
paintutils.drawPixel(16,9,17)
paintutils.drawPixel(17,9,17)
paintutils.drawPixel(14,10,17)
paintutils.drawPixel(17,10,17)
paintutils.drawPixel(14,11,17)
paintutils.drawPixel(15,11,17)
paintutils.drawPixel(16,11,17)
paintutils.drawPixel(17,11,17)
———————-
paintutils.drawPixel(14,14,9)
paintutils.drawPixel(15,14,9)
paintutils.drawPixel(16,14,9)
paintutils.drawPixel(17,14,9)
paintutils.drawPixel(14,15,9)
paintutils.drawPixel(17,15,9)
paintutils.drawPixel(14,16,9)
paintutils.drawPixel(15,16,9)
paintutils.drawPixel(16,16,9)
paintutils.drawPixel(17,16,9)
—————
paintutils.drawLine(1, 1, 51, 1, tonumber(farbe))
fg.close()





————


term.setTextColor(colors.white)
shell.run"cd //"
file = fs.open("loginSettins/curUser", "r")
version = file.readLine()
file.close()
term.setCursorPos(1,1)
print(version)
shell.run"cd //"
shell.run"cd OS"

term.setBackgroundColor(colors.lightGray)
term.setTextColor(colors.white)
local time = os.time()
local formattedTime = textutils.formatTime(time, false)
term.setCursorPos(44,1)
print(formattedTime)


valithor #9
Posted 13 April 2015 - 10:54 PM
Could you not have made another topic altogether instead?

Anyways, colors.farbe does not exist in the colors api.
And since farbe is a string, you need to convert it into a number.
So paintutils.drawPixel(1,5, tonumber(farbe)) would be the way to go.

doenst work it always shows lightBlue

Code
Spoiler


shell.run"clear"
——————————————————
Bild = paintutils.loadImage("OS/d1.img")
paintutils.drawImage(Bild,1,1)
——————————————————
term.setBackgroundColor(colors.lightGray)
term.setTextColor(colors.white)
—–
fg = fs.open("loginSettins/version","r")
farbe = fg.readLine()
——————————————————
paintutils.drawPixel(49,17, colors.blue)
paintutils.drawPixel(50,18, colors.blue)
paintutils.drawPixel(50,17, colors.blue)
paintutils.drawPixel(49,18, colors.blue)
paintutils.drawPixel(48,17, colors.blue)
paintutils.drawPixel(48,18, colors.blue)






term.setTextColor(colors.gray)
term.setBackgroundColor(colors.white)
term.setCursorPos(1,7)
print("Settings")
term.setCursorPos(2,12)
print(" News")
term.setCursorPos(2,17)
print(" Worm")
term.setCursorPos(11,7)
print("ChromeMenu")
term.setCursorPos(13,12)
print("NPaint")
term.setCursorPos(13,17)
print("XFiles")
term.setTextColor(colors.white)
term.setBackgroundColor(colors.blue)
term.setCursorPos(48,17)
print("")
term.setCursorPos(48,18)



paintutils.drawPixel(3,4,40000)
paintutils.drawPixel(4,4,40000)
paintutils.drawPixel(5,4,40000)
paintutils.drawPixel(6,4,40000)
paintutils.drawPixel(3,5,40000)
paintutils.drawPixel(6,5,40000)
paintutils.drawPixel(3,6,40000)
paintutils.drawPixel(4,6,40000)
paintutils.drawPixel(5,6,40000)
paintutils.drawPixel(6,6,40000)
————
paintutils.drawPixel(3,9,3000)
paintutils.drawPixel(4,9,3000)
paintutils.drawPixel(5,9,3000)
paintutils.drawPixel(6,9,3000)
paintutils.drawPixel(3,10,3000)
paintutils.drawPixel(6,10,3000)
paintutils.drawPixel(3,11,3000)
paintutils.drawPixel(4,11,3000)
paintutils.drawPixel(5,11,3000)
paintutils.drawPixel(6,11,3000)
—————
paintutils.drawPixel(3,14,20000)
paintutils.drawPixel(4,14,20000)
paintutils.drawPixel(5,14,20000)
paintutils.drawPixel(6,14,20000)
paintutils.drawPixel(3,15,20000)
paintutils.drawPixel(6,15,20000)
paintutils.drawPixel(3,16,20000)
paintutils.drawPixel(4,16,20000)
paintutils.drawPixel(5,16,20000)
paintutils.drawPixel(6,16,20000)
————–
paintutils.drawPixel(14,4,33)
paintutils.drawPixel(15,4,33)
paintutils.drawPixel(16,4,33)
paintutils.drawPixel(17,4,33)
paintutils.drawPixel(14,5,33)
paintutils.drawPixel(17,5,33)
paintutils.drawPixel(14,6,33)
paintutils.drawPixel(15,6,33)
paintutils.drawPixel(16,6,33)
paintutils.drawPixel(17,6,33)
——————-
paintutils.drawPixel(14,9,17)
paintutils.drawPixel(15,9,17)
paintutils.drawPixel(16,9,17)
paintutils.drawPixel(17,9,17)
paintutils.drawPixel(14,10,17)
paintutils.drawPixel(17,10,17)
paintutils.drawPixel(14,11,17)
paintutils.drawPixel(15,11,17)
paintutils.drawPixel(16,11,17)
paintutils.drawPixel(17,11,17)
———————-
paintutils.drawPixel(14,14,9)
paintutils.drawPixel(15,14,9)
paintutils.drawPixel(16,14,9)
paintutils.drawPixel(17,14,9)
paintutils.drawPixel(14,15,9)
paintutils.drawPixel(17,15,9)
paintutils.drawPixel(14,16,9)
paintutils.drawPixel(15,16,9)
paintutils.drawPixel(16,16,9)
paintutils.drawPixel(17,16,9)
—————
paintutils.drawLine(1, 1, 51, 1, tonumber(farbe))
fg.close()





————


term.setTextColor(colors.white)
shell.run"cd //"
file = fs.open("loginSettins/curUser", "r")
version = file.readLine()
file.close()
term.setCursorPos(1,1)
print(version)
shell.run"cd //"
shell.run"cd OS"

term.setBackgroundColor(colors.lightGray)
term.setTextColor(colors.white)
local time = os.time()
local formattedTime = textutils.formatTime(time, false)
term.setCursorPos(44,1)
print(formattedTime)



Your problem is when you read the file it is returning a string not the table entry colors.red points to.

To fix this you could do something like this.


color = "colors.red" -- this is what you are getting from the file
color = loadstring("return "..color)()
Bomb Bloke #10
Posted 14 April 2015 - 12:16 AM
I thought the file merely contained the word "red", in which case you'd do:

local fg = fs.open("loginSettins/version","r")
local farbe = fg.readLine()
paintutils.drawPixel(1,5,colors[farbe])
fg.close()
jakejakey #11
Posted 14 April 2015 - 02:49 AM
Please delete this post i realized that i had mispelled some things in my code
Edited on 14 April 2015 - 02:08 AM
HPWebcamAble #12
Posted 14 April 2015 - 03:07 AM
f = fs.open("file","r")
file = {}
i = 0
repeat
curLine = f.readLine()
if curLine == nil then
else
file[i] = curLine
end
until curLine == nil

Bomb bloke suggested this the other day:

local f = fs.open("file","r")
local file = {}
for line in f.readLine do
  table.insert(file,line)
end
f.close()

Just a bit shorter, does the same thing

Edit: Actually, jake, yours has a few types, you might want to check yours again
Edit2: Type fix! Removed ()
Edited on 14 April 2015 - 02:28 AM
jakejakey #13
Posted 14 April 2015 - 04:08 AM
ah okay im gonna remove my post then
Bomb Bloke #14
Posted 14 April 2015 - 04:22 AM
Edit: Actually, jake, yours has a few types, you might want to check yours again

Did I really write it like that? I hope not, 'cause that won't work… you'd need to use f.readLine there, as opposed to f.readLine().

But it turns out the io API has io.lines(), which handles the file opening / closing for you:

local file = {}
for line in io.lines("file") do table.insert(file,line) end

… but still, I was under the impression that Leon's line only had the one line in it. For a single-line file you might even be able to crush it down to something like:

local myLine = io.lines("file")()
HPWebcamAble #15
Posted 14 April 2015 - 04:33 AM
Did I really write it like that? I hope not, 'cause that won't work… you'd need to use f.readLine there, as opposed to f.readLine().

D: Sorry I wrote it from memory, I fixed that in my post

local myLine = io.lines("file")()

Seriously? Haha one day we'll compact programs so much they'll just be a single character

Spoiler

a
Boom OS right there

Note: Few bugs to work out
Edited on 14 April 2015 - 02:34 AM