Jump to content




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


  • You cannot reply to this topic
4 replies to this topic

#1 nelson55

  • New Members
  • 2 posts

Posted 15 October 2012 - 05: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

#2 JoshhT

  • New Members
  • 64 posts
  • LocationAustralia

Posted 15 October 2012 - 05: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.

#3 Kazimir

  • Members
  • 90 posts
  • Locationthat's no moon...

Posted 15 October 2012 - 06: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


#4 JoshhT

  • New Members
  • 64 posts
  • LocationAustralia

Posted 15 October 2012 - 06:26 AM

View PostKazimir, on 15 October 2012 - 06:15 AM, said:

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.

#5 nelson55

  • New Members
  • 2 posts

Posted 15 October 2012 - 07:18 PM

Thank you! It works <3





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users