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

[Error] Problem with IO?

Started by ZeroLRS, 17 August 2012 - 04:10 AM
ZeroLRS #1
Posted 17 August 2012 - 06:10 AM
Not sure if the problem is with io or my fs table.

I am writing a basic program to transfer all of the files from one computer to another. This will eventually include whatever is on the disk, but for now, it just does the root directory.

At least is should.

I am getting an "attempt to index? (a nil value)" at line 9 of this code (the client sending the files):


args = {...}
rednet.open("top")
x = 1
FileList = fs.list("")
rednet.send(tonumber(args[1]), tostring(#FileList))
for _,fileName in ipairs(FileList) do
file = io.open(fileName, "r")
s = file:read("*a")
rednet.send(tonumber(args[1]), s)
file:close()
print("File ", x , " sent.")
end
rednet.close("top")


also, here is the server (which receives the files):


args = {...}
rednet.open("top")
print("Rednet opened on top")
sid1, number = rednet.receive()
sid2, file = rednet.receive()
print("Files receiving from ", sid1)
if tonumber(args[1]) == sid1 then
print("sid matched the argument")
for n = 1, number do
  sid2, fileContent = rednet.receive()
  file = io.open(tonumber(n), "w")
  if file then
   file:write(fileContent)
   file:close()
   print("File ", n, " of ", number, " received sucessfully!")
  end
end
rednet.close("top")
print("Rednet Closed!")
end


The client is run by "<program name> <id of server>"
The server is run by "<program name> <id of client>"

Any and all help is appreciated, weather it is about my syntax, program etiquette, or anything else.

Lastly, please do not just post the answer for me. I picked up computercraft to expand my programming knowledge, not to be hand-fed.
I am here to learn, not to be told.

Thank you,
ZeroLRS
Luanub #2
Posted 17 August 2012 - 06:26 AM
Check and make sure that the file you are trying to send is available. I don't think the files are being opened and captured in the filename var.

Edit: I think its due to the fact that fs.list() will also show directories. Add a check to see if fileName is a directory or not. See if that helps if not let me know and I'll setup a way to test it.


if not fs.isDir(fileName) then
--code to send file
end
ZeroLRS #3
Posted 17 August 2012 - 03:46 PM
Thank you! This solved my problem. It was trying to send directories and they were returning nil.