419 posts
Location
your hardrive
Posted 25 November 2012 - 09:14 AM
rednet.open("top")
write ("file name:")
local filename = read()
file = fs.open(filename,"w")
while true do
local id,msg,distance = rednet.receive()
file.writeLine(msg)
if msg == "renderComplete" then
break
end
file.close()
print ("render complete")
rednet.close ("top")
end
this program doesnt write to the file or the loop doesnt break the other program sending the message is working fine i set up a rednet sniffer and it works
536 posts
Posted 25 November 2012 - 09:20 AM
how about;
rednet.open("top")
write ("file name:")
local filename = read()
if string.gsub(filename, " ", "") ~= "" then
if fs.exists(filename) then fs.delete(filename) end
local file = fs.open(filename,"w")
local ok = true
while ok do
local id,msg,distance = rednet.receive()
if msg ~= nil then
file.writeLine(msg.."n")
if msg == "renderComplete" then
ok = false
end
end
end
file.close()
print ("render complete")
rednet.close ("top")
end
you also had an extra end on the bottom of the code, I presume this is from a function and you accidentally copied an extra end but either way, I got rid of it :D/>/>/>
this also checks to see if the input was empty (only contains spaces or nothing)
419 posts
Location
your hardrive
Posted 25 November 2012 - 09:26 AM
thanks ill test this out and give you credit when i release this and the other side of the program
EDIT: i tried yours it didnt work my computer keeps coping it wrong so all the code is on one line i may have not fixed it right
2088 posts
Location
South Africa
Posted 25 November 2012 - 01:53 PM
billysback's code was missing one end; try this?
rednet.open("top")
write ("file name:")
local filename = read()
if string:gsub(filename, " ", "") ~= "" then
if fs.exists(filename) then fs.delete(filename) end
local file = fs.open(filename,"w")
local ok = true
while ok do
local id,msg,distance = rednet.receive()
if msg ~= nil then
file.writeLine(msg)
if msg == "renderComplete" then
ok = false
end -- forgot this end
end
end
file.close()
print ("render complete")
rednet.close ("top")
end
536 posts
Posted 25 November 2012 - 09:59 PM
you can do if statements without an end using white space, I may have accidentally added a then in though (I forget if they are allowed)
also, if all the code is on one line try doing:
file.writeLine(msg.."n")
EDIT:
I editied my original code with some fixes in it (string:gsub changed to string.gsub, msg.."n" instead of msg, added an end.
1548 posts
Location
That dark shadow under your bed...
Posted 26 November 2012 - 12:01 AM
the problem was that you are ending your while loop
after you put the closing statements (where you close the file etc) so when you break the loop it doesn't close the file so it doesn't save input… here is your corrected code
rednet.open("top")
write ("file name:")
local filename = read()
file = fs.open(filename,"w")
while true do
local id,msg,distance = rednet.receive()
file.writeLine(msg)
if msg == "renderComplete" then
file.close()
print ("render complete")
rednet.close ("top")
break
end
end
or
rednet.open("top")
write ("file name:")
local filename = read()
file = fs.open(filename,"w")
while true do
local id,msg,distance = rednet.receive()
file.writeLine(msg)
if msg == "renderComplete" then
break
end
end
file.close()
print ("render complete")
rednet.close ("top")
2005 posts
Posted 26 November 2012 - 07:17 AM
Forgive me if I'm mistaken here, but I think that you want to receive multiple rednet messages and save them all into a single file, right? If that's the case, then you probably want to use "fs.open(filename,"a")" to open the file in apphend mode. Or you need to concatenate the new message to the old string every loop until you have the full file string, and then save that to the file in a single command. By opening the file in write mode and repeatedly using writeLine(), you'll overwrite the existing file contents every single time and only ever end up with the last line you sent. Which might have been the undesired behavior described.