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

Help! sendfile:31: bad argument: string expected, got nil

Started by nelson55, 15 October 2012 - 04:00 AM
nelson55 #1
Posted 15 October 2012 - 06:00 AM
I cannot figure out why this isnt working. I got the program to work, up to where the file is sending, and i get this error. I am trying to set this up to send files over rednet. The receive code works, but the sending one doesnt yet :/ any help would be appreciated :D/>/>

Here is the sending code with the error:

local args = {...}
local sides = {"top","bottom","back","front","left","right"}
for i=1,#sides do
if not rednet.isOpen(sides[i]) then
  rednet.open(sides[i])
end
end
term.setCursorPos(1,1)
term.clear()
if #args < 1 then
print("Usage (<> required, [] optional):")
print("sendfile [ComputerID] <file>")
return
end
if #args == 1 then
if fs.exists(args[1]) then
  textutils.slowPrint("Sending file "..args[1].."!")
  rednet.broadcast("RECEIVE")
  rednet.broadcast(args[1])
  file = fs.open(args[1], "r")
  rednet.broadcast(file.readAll())
  file.close()
else
  print(args[1].." does not exist!")
  return
end
else
if fs.exists(args[2]) then
  textutils.slowPrint("Sending file "..args[2].." to "..args[1])
  F = fs.open(args[2])
  id = tonumber(args[1])
  rednet.send(id, "RECEIVE")
  rednet.send(id, args[2])
  rednet.send(id, F.readAll())
  F.close()
else
  print(args[2].." does not exist!")
  return
end
end

And here is the working receive code:

term.clear()
term.setCursorPos(1,1)
local sides = {"left","right","top","bottom","back","front"}
for i=1,#sides do
if not rednet.isOpen(sides[i]) then
  rednet.open(sides[i])
end
end
while true do
id,msg = rednet.receive()
if msg == "RECEIVE" then
  id2,msg2 = rednet.receive()
  f = fs.open(msg2, "w")
  id3,msg3 = rednet.receive()
  f.write(msg3)
  f.close()
  print("File "..msg2.." received!")
end
end

Thanks
JoshhT #2
Posted 15 October 2012 - 06:56 AM
I had this same problem yesterday writing my lineFinder program, but I can't remember how I solved it :D/>/>
If I remember I'll be sure to post here. I know it's got something to do with the args[1] thing. Give me a little bit I'll try remember.
Kazimir #3
Posted 15 October 2012 - 07:15 AM
You forgot after args[2] write "r" in line 36
local args = {...}
local sides = {"top","bottom","back","front","left","right"}

for i=1,#sides do
    if not rednet.isOpen(sides[i]) then
        rednet.open(sides[i])
    end
end

term.setCursorPos(1,1)
term.clear()

if #args < 1 then
    print("Usage (<> required, [] optional):")
    print("sendfile [ComputerID] <file>")
    return
end

function WR()
    if fs.exists(args[1]) then
        textutils.slowPrint("Sending file "..args[1].."!")
        rednet.broadcast("RECEIVE")
        rednet.broadcast(args[1])
        file = fs.open(args[1], "r")
        rednet.broadcast(file.readAll())
        file.close()
    else
        print(args[1].." does not exist!")
        return
    end
end

function WS()
    if fs.exists(args[2]) then
        textutils.slowPrint("Sending file "..args[2].." to "..args[1])
        F = fs.open(args[2], "r")
        id = tonumber(args[1])
        rednet.send(id, "RECEIVE")
        rednet.send(id, args[2])
        rednet.send(id, F.readAll())
        F.close()
    else
        print(args[2].." does not exist!")
        return
    end
end

if #args == 1 then WR() else WS() end
JoshhT #4
Posted 15 October 2012 - 07:26 AM
You forgot after args[2] write "r" in line 36

You're on it mate. I always seem to forget to define the "mode" to open the file.
nelson55 #5
Posted 15 October 2012 - 08:18 PM
Thank you! It works <3