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

Not really sure what the problem is :/ [Solved]

Started by Engineer, 19 March 2013 - 04:16 AM
Engineer #1
Posted 19 March 2013 - 05:16 AM
Hello,

I am stumped at this code. If I individually test the function getPastebin( code, path ) it works fine, but in this context it doesnt for some reason.
It tries to call nil at: "file.write( contents )"


local tPB = {
	[1] = { "anypastebincode", "path" },
	[2] = { "anypastebincode", "path" },
	[3] = { "anypastebincode", "path" }
}

function getPastebin( code, path )
	local response = http.get(
		"http://pastebin.com/raw.php?i=" .. textutils.urlEncode( code )
		)
	if response then
		local contents = response.readAll()
		local file = fs.open( path, "w" )
		file.write( contents )
		file.close()
		return true
	else
		return false
	end
end
for i = 1, #tPB do
		if getPastebin( tPB[i][1], tPB[i][2] ) then
				bar( step ) -- This isnt the problem
		else
			fail = true
			break
		end
	end
end

About the table, it has all different codes and paths, but its leading to kind of a secret project. And yes, the getPastebin function is a rip-off of the original pastebin program, I just dont want the text that the program produces :P/>/>

Thank you very much in advance,

Engineer

Edit: I just needed to create the folders before I even wanted to write to them.
Edited on 19 March 2013 - 04:50 AM
Kingdaro #2
Posted 19 March 2013 - 05:26 AM
If a folder for a file you're trying to create doesn't exist, you have to make the folder it's in first. For example, if your path is "somefolder/afile" and somefolder doesn't exist, you can't write to afile.

If that's not the problem, then I'm not sure.
Engineer #3
Posted 19 March 2013 - 05:49 AM
Thanks Kingdaro, I assumed it would auto create, just like you write to a file. Apparantly it does not.

Thank you!
JokerRH #4
Posted 19 March 2013 - 09:43 AM
If you open a file in write mode and it doesn't exist, it will create it.
Your problem must be somewhere else…:P/>/>
(At least it works on my CC computer)