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

SOLVED

Started by ComputerCraftFan11, 12 May 2012 - 08:00 PM
ComputerCraftFan11 #1
Posted 12 May 2012 - 10:00 PM
I made a file but now I can't write to it because it errors.

Can someone help?

local x,y = term.getSize()
rednet.open("top")
local EditingValue = "";
function rednetV()
return "2.3"
end
title = "Rednet Explorer " ..rednetV() --Add title = "name" to change the webpage's title!
local website = "home";
if fs.exists(".cache") then fs.delete(".cache") end
if fs.exists(".websiteedited") then fs.delete(".websiteedited") end
fs.makeDir(".cache")
local cPrint = function(text)
local x2,y2 = term.getCursorPos()
term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)

print(text)
end
function createSite(websitename)
	 fs.delete("startup")
	 startup = fs.open("startup", "w")
	 startup.writeLine("websitename = \"" ..websitename.. "\"")
	 servercode = [[
	
	
	  function record(text)
	   print(text)
	   log = fs.open("rednet.log", "w")
	   log.writeLine(text)
	   log.close()
	  end
	
	  local x,y = term.getSize()
	  local cPrint = function(text)
	   local x2,y2 = term.getCursorPos()
	   term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
	  
	   print(text)
	  end
	
	  rednet.open("top")
	  term.clear()
	  cPrint("Hosting " ..websitename.. "...\n")
	  cPrint("Go to " ..websitename.. "/editor to edit it! (PASS: " ..os.getComputerID().. ")\n")
	  test = fs.open(websitename, "r")
	  fileContents = test:readAll()
	  test.close()
	  while true do
	   sleep(0)
	   id, message = rednet.receive()
	   if message == websitename then
		record("   [" ..os.time().."] [" ..id.. "] Pinged Website.")
		rednet.send(id, fileContents)
		record("   [" ..os.time().."] [" ..id.. "] Received Data")
	   elseif message == websitename.. "/editor" then
		rednet.send(id, "EditorMode")
		rednet.send(id, tostring(os.getComputerID()))
		rednet.send(id, fileContents)
		record("   [" ..os.time().."] [" ..id.. "] Editor Opened.")
	   elseif message == websitename.. " EDITED COMPLED!" then
		id, message = rednet.receive(0.001)
		fs.delete(websitename)
		webpage = fs.open(websitename, "w")
		webpage.write(message)
		webpage.close()
	  
	  
		test = fs.open(websitename, "r")
		fileContents = test:readAll()
		test.close()
		record("   [" ..os.time().."] [" ..id.. "] Updated Website!")
	   end
	  end
	 ]]
	
	 startup.writeLine(servercode)
	 startup.close()
	 os.reboot()
end
local Address = function()
text = "rdnt://"
term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), 2)
term.clearLine()

write("rdnt://")
website = read()
loadWebpage()
end
function done()
term.setCursorPos(1, y)
name = "F5 = Refresh"
write("Press CTRL to travel the web! :P/>/>")
term.setCursorPos(x - name:len(), y)
write(name)

while true do
  sleep(0) -- stop crashing
  e, k = os.pullEvent("key")
  if k == 63 then
   loadWebpage()
   break
  elseif k == 29 then
   Address()
   break
  end
end
end
loadWebpage = function()
term.clear()
term.setCursorPos(1,1)
cPrint(title)
cPrint("rdnt://" ..website.. "\n")
if website == "home" then
  print("Welcome to RedNet explorer (2.0)! This requires you to have a wireless modem on your computer.")
  print("Host a website at:				")
  print("rdnt://newsite!			")
  print("   -ComputerCraftFan11	")
elseif website == "newsite" then
  print("Are you sure you would like to make this PC a server?")
  cPrint("Y = Yes N = No")
  while true do
   e, k = os.pullEvent("char")
   if k == "y" or k == "n" then
	break
   end
  end

  if k == "y" then
   term.clear()
   term.setCursorPos(1,1)
   title = "Rednet Servers " ..rednetV()
   cPrint(title)
   print("Welcome to the Rednet Servers. Please enter the website name: ")
   websitename = read()
   rednet.broadcast(websitename)
   i, me = rednet.receive(0.001)
   if me == nil then
	print("Thank you! This website will be running off of the file:\n" ..websitename.. "\n")
	write("Are you sure? (Y = Continue, V = Edit)")
	input = read()
	if input == "Y" or input == "y" then
	 if fs.exists(websitename) == false then
	  print("Please create " ..websitename.. " first!")
	  sleep(0.5)
	  shell.run("edit", websitename)
	 end
	 term.clear()
	 term.setCursorPos(1,1)
	 createSite(websitename)
	elseif input == "V" or input == "v" then
	 shell.run("edit", websitename)
	 term.clear()
	 term.setCursorPos(1,1)
	 createSite(websitename)
	end
   else
	print("I'm sorry, this domain name is taken.")
   end
  end
else
  title = "Rednet Explorer " ..rednetV()
  rednet.broadcast(website)
  print("Connecting...")
  id, message = rednet.receive(0.1)
  if message == nil then
   print("Unable to load webpage.")
  elseif message == "EditorMode" then
   id, password = rednet.receive(0.1)
   id, EditingValue = rednet.receive(0.1)
   write("Password: ")
   input = read("*")
   if tostring(password) == input then
	fs.delete(".cache/" ..website)
	editor = fs.open(".websiteedited", "w")
	edtior.writeLine(EditingValue)
	editor.close()
  
	shell.run("edit", ".cache/" ..website)
  
	edited = fs.open(".cache/" ..website, "r")
	editwebpage = edited.readAll()
	edited.close()
  
	rednet.broadcast(website.. " EDITED COMPLED!")
	rednet.broadcast(editwebpage)
	website = "home"
	loadWebpage()
  
   else
  
	website = "home"
	loadWebpage()
  
   end
  else
   if fs.exists(".cache/" ..website) then fs.delete(".cache/" ..website) end
   webpage = fs.open(".cache/" ..website, "w")
   webpage.write(message)
   webpage.close()
   term.clear()
   term.setCursorPos(1,1)
   cPrint(title)
   cPrint("rdnt://" ..website.. "\n")
   shell.run(".cache/" ..website)
  end
end

done()
end
loadWebpage()

Error:

f:182: attempt to index ? (a nil value)
at

	editor = fs.open(".websiteedited", "w")
	edtior.writeLine(EditingValue)
	editor.close()
MysticT #2
Posted 12 May 2012 - 10:18 PM
Always remember to close your file handles, cause if you don't this will happen. If it's on ssp, just close minecraft and open it again, it should fix it. If you are on a server, I think it must be restarted to fix this.
I hope someone finds another solution, but I don't think there is.
ComputerCraftFan11 #3
Posted 12 May 2012 - 10:20 PM
Always remember to close your file handles, cause if you don't this will happen. If it's on ssp, just close minecraft and open it again, it should fix it. If you are on a server, I think it must be restarted to fix this.
I hope someone finds another solution, but I don't think there is.

OH, it crashes right after it writes stuff into the file so it doesn't ever reach the close()

Why does it crash? D:
MysticT #4
Posted 12 May 2012 - 10:27 PM
The second error is because you can't open the file, so it returns nil. That's why it says "attempt to index ? (a nil value)".
Always check the handle before using it:

local file = fs.open("path", "mode")
if file then
  -- read/write file
  file.close() -- always close the handle
end
ComputerCraftFan11 #5
Posted 12 May 2012 - 10:31 PM
The second error is because you can't open the file, so it returns nil. That's why it says "attempt to index ? (a nil value)".
Always check the handle before using it:

local file = fs.open("path", "mode")
if file then
  -- read/write file
  file.close() -- always close the handle
end

I get the same error

local x,y = term.getSize()
rednet.open("top")
local EditingValue = "";
function rednetV()
return "2.3"
end
title = "Rednet Explorer " ..rednetV() --Add title = "name" to change the webpage's title!
local website = "home";
if fs.exists(".cache") then fs.delete(".cache") end
if fs.exists(".websiteedited") then fs.delete(".websiteedited") end
fs.makeDir(".cache")
local cPrint = function(text)
local x2,y2 = term.getCursorPos()
term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)

print(text)
end
function createSite(websitename)
	 fs.delete("startup")
	 startup = fs.open("startup", "w")
	 startup.writeLine("websitename = "" ..websitename.. """)
	 servercode = [[
	 
	 
	  function record(text)
	   print(text)
	   log = fs.open("rednet.log", "w")
	   log.writeLine(text)
	   log.close()
	  end
	 
	  local x,y = term.getSize()
	  local cPrint = function(text)
	   local x2,y2 = term.getCursorPos()
	   term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
	  
	   print(text)
	  end
	 
	  rednet.open("top")
	  term.clear()
	  cPrint("Hosting " ..websitename.. "...n")
	  cPrint("Go to " ..websitename.. "/editor to edit it! (PASS: " ..os.getComputerID().. ")n")
	  test = fs.open(websitename, "r")
	  fileContents = test:readAll()
	  test.close()
	  while true do
	   sleep(0)
	   id, message = rednet.receive()
	   if message == websitename then
	    record("   [" ..os.time().."] [" ..id.. "] Pinged Website.")
	    rednet.send(id, fileContents)
	    record("   [" ..os.time().."] [" ..id.. "] Received Data")
	   elseif message == websitename.. "/editor" then
	    rednet.send(id, "EditorMode")
	    rednet.send(id, tostring(os.getComputerID()))
	    rednet.send(id, fileContents)
	    record("   [" ..os.time().."] [" ..id.. "] Editor Opened.")
	   elseif message == websitename.. " EDITED COMPLED!" then
	    id, message = rednet.receive(0.001)
	    fs.delete(websitename)
	    webpage = fs.open(websitename, "w")
	    webpage.write(message)
	    webpage.close()
	   
	   
	    test = fs.open(websitename, "r")
	    fileContents = test:readAll()
	    test.close()
	    record("   [" ..os.time().."] [" ..id.. "] Updated Website!")
	   end
	  end
	 ]]
	
	 startup.writeLine(servercode)
	 startup.close()
	 os.reboot()
end
local Address = function()
text = "rdnt://"
term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), 2)
term.clearLine()

write("rdnt://")
website = read()
loadWebpage()
end
function done()
term.setCursorPos(1, y)
name = "F5 = Refresh"
write("Press CTRL to travel the web! :P/>/>")
term.setCursorPos(x - name:len(), y)
write(name)

while true do
  sleep(0) -- stop crashing
  e, k = os.pullEvent("key")
  if k == 63 then
   loadWebpage()
   break
  elseif k == 29 then
   Address()
   break
  end
end
end
loadWebpage = function()
term.clear()
term.setCursorPos(1,1)
cPrint(title)
cPrint("rdnt://" ..website.. "n")
if website == "home" then
  print("Welcome to RedNet explorer (2.0)! This requires you to have a wireless modem on your computer.")
  print("Host a website at:			    ")
  print("rdnt://newsite!		    ")
  print("   -ComputerCraftFan11    ")
elseif website == "newsite" then
  print("Are you sure you would like to make this PC a server?")
  cPrint("Y = Yes N = No")
  while true do
   e, k = os.pullEvent("char")
   if k == "y" or k == "n" then
    break
   end
  end
 
  if k == "y" then
   term.clear()
   term.setCursorPos(1,1)
   title = "Rednet Servers " ..rednetV()
   cPrint(title)
   print("Welcome to the Rednet Servers. Please enter the website name: ")
   websitename = read()
   rednet.broadcast(websitename)
   i, me = rednet.receive(0.001)
   if me == nil then
    print("Thank you! This website will be running off of the file:n" ..websitename.. "n")
    write("Are you sure? (Y = Continue, V = Edit)")
    input = read()
    if input == "Y" or input == "y" then
	 if fs.exists(websitename) == false then
	  print("Please create " ..websitename.. " first!")
	  sleep(0.5)
	  shell.run("edit", websitename)
	 end
	 term.clear()
	 term.setCursorPos(1,1)
	 createSite(websitename)
    elseif input == "V" or input == "v" then
	 shell.run("edit", websitename)
	 term.clear()
	 term.setCursorPos(1,1)
	 createSite(websitename)
    end
   else
    print("I'm sorry, this domain name is taken.")
   end
  end
else
  title = "Rednet Explorer " ..rednetV()
  rednet.broadcast(website)
  print("Connecting...")
  id, message = rednet.receive(0.1)
  if message == nil then
   print("Unable to load webpage.")
  elseif message == "EditorMode" then
   id, password = rednet.receive(0.1)
   id, EditingValue = rednet.receive(0.1)
   write("Password: ")
   input = read("*")
   if tostring(password) == input then
    fs.delete(".cache/" ..website)
    editor = fs.open(".websiteedited", "w")
    if editor then
	 edtior.writeLine(EditingValue)
	 editor.close()
    end
   
    shell.run("edit", ".cache/" ..website)
   
    edited = fs.open(".cache/" ..website, "r")
    editwebpage = edited.readAll()
    edited.close()
   
    rednet.broadcast(website.. " EDITED COMPLED!")
    rednet.broadcast(editwebpage)
    website = "home"
    loadWebpage()
   
   else
   
    website = "home"
    loadWebpage()
   
   end
  else
   if fs.exists(".cache/" ..website) then fs.delete(".cache/" ..website) end
   webpage = fs.open(".cache/" ..website, "w")
   webpage.write(message)
   webpage.close()
   term.clear()
   term.setCursorPos(1,1)
   cPrint(title)
   cPrint("rdnt://" ..website.. "n")
   shell.run(".cache/" ..website)
  end
end

done()
end
loadWebpage()

at

    editor = fs.open(".websiteedited", "w")
    if editor then
	 edtior.writeLine(EditingValue)
	 editor.close()
    end
Luanub #6
Posted 12 May 2012 - 10:40 PM
Looks like a typo edtior.writeLine(EditingValue)

And I would do it like this so it always closes the handle


editor = fs.open(".websiteedited", "w")
if editor then
  editor.writeLine(EditingValue)
end
editor.close()
Edited on 12 May 2012 - 08:42 PM
ComputerCraftFan11 #7
Posted 12 May 2012 - 10:46 PM
Thank! I can't believe I missed that!
MysticT #8
Posted 12 May 2012 - 10:57 PM
Looks like a typo edtior.writeLine(EditingValue)

And I would do it like this so it always closes the handle


editor = fs.open(".websiteedited", "w")
if editor then
  editor.writeLine(EditingValue)
end
editor.close()
Ups, missed that (I didn't check for typos :P/>/>).
But what you suggest to close handles is wrong, if you do that and the handle returned is nil, it would throw an error ("attempt to index a nil value"). Closing it inside the if statement would be the right way to do it.