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

program not writing to files

Started by ETHANATOR360, 25 November 2012 - 08:14 AM
ETHANATOR360 #1
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
billysback #2
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)
ETHANATOR360 #3
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
remiX #4
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
billysback #5
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.
KaoS #6
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")
ChunLing #7
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.