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

Help with rednet, and io.open

Started by XxLegendxX98, 13 July 2012 - 02:57 PM
XxLegendxX98 #1
Posted 13 July 2012 - 04:57 PM
I'm attempting to make a factory, with a terminal that you can place orders to buy an item from in a server. I'm attempting to have one computer keep track of all the items, and once it gets a rednet message, it will send the items list to the computer wanting to buy. But I'm having trouble with io.open, and I haven't used rednet in a while so there may be mistakes with it.

This is the code for the purchasing terminal

rednet.open("top")
rednet.send(449, "getItems")
rednet.receive(10)
local file = (rednet_message)
io.open("items", "w+")
(file)
file:close
shell.run("purchase")

And this is for the receiving terminal

rednet.open("top")
rednet.receive()
if (rednet_message) = "getItems" then
local file = fs.open("items", "r")
rednet.send(418, (file))
end
os.reboot()
MysticT #2
Posted 13 July 2012 - 05:04 PM
First, you need to store the values returned by rednet.receive to use them:

local id, msg = rednet.receive()
Then, to read the file:

local file = fs.open("FileName", "r")
if file then
  local contents = file.readAll()
  -- now contents is a string with all the file contents
  -- you can also read it line by line using file.readLine()
  file.close() -- always close the files
end
To write to a file:

local file = fs.open("FileName", "w")
if file then
  -- write to file here
  file.write("Some string here (you can use a string variable)")
  file.close() -- again, always close the files
end
XxLegendxX98 #3
Posted 13 July 2012 - 05:11 PM
First, you need to store the values returned by rednet.receive to use them:

local id, msg = rednet.receive()
Then, to read the file:

local file = fs.open("FileName", "r")
if file then
  local contents = file.readAll()
  -- now contents is a string with all the file contents
  -- you can also read it line by line using file.readLine()
  file.close() -- always close the files
end
To write to a file:

local file = fs.open("FileName", "w")
if file then
  -- write to file here
  file.write("Some string here (you can use a string variable)")
  file.close() -- again, always close the files
end
Oh, Thanks. This will help a lot.