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

Error when writing to a file

Started by crazyadam0, 22 May 2013 - 09:41 PM
crazyadam0 #1
Posted 22 May 2013 - 11:41 PM
I am busy working away on my new OS, and I encountered annother problem, that i have NO IDEA how to solve. The error is "winMC:29: attempt to index ? (a nil value)" Where is the nil in file.write(contents); ?
BY THE WAY: the pastebinGet function was copied from suicidalstdz Ender OS, I GIVE FULL CREDIT TO HIM FOR MAKING THIS FUNCTION.
here is my code:


-- logo for windows MC
local w,h = term.getSize();
local col = term.setTextColor;
local bgc = term.setBackgroundColor;
local user;
local userDir;
		
--logo = {"				  __   __		  __  ";  IGNORE
			 --	"|	   | | |\   | |  \ /  \ |	| /	";   THIS
			 --	"|	   | | | |  | |  | |  | |	| |	"; <
			 --	"|	   | | | \  | |  | |  | |	| \_   ";
			 --	"|   |   | | |  | | |  | |  | |	|   \  ";
			 --	"\__/ \__/ | |   \| |__/ \__/  \/\/  __/  ";
			 --	"										 ";
			 --	"				  M C					";
		
  --	}-- END LOGO
logo = {
		"WINDOWS";
		"  M C  ";
	   }
function pastebinGet(code,path)
local sPath = shell.resolve(path);
local response = http.get("http://pastebin.com/raw.php?i="..textutils.urlEncode(code));
if response then
local contents = response.readAll();
response.close();
local file = fs.open(sPath,"w");
file.write(contents); -- THIS IS LINE 29 WHERE THE ERROR IS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
file.close();
return true;
else
return false;
end
end
function checkOsReq()
term.setCursorPos(0,1);
--check for color suporting computer
if not term.isColor() then
  error("CANNOT LOAD; MUST BE RAN ON AN ADVANCED COMPUTER");
end
--check for winMC directory
pastebinGet("XjvCmnjQ","/.winMC/.OS/checkOSR");
shell.run("/.winMC/.OS/checkOSR");
end
function createProfile()
local next = false;
local usrn;
local pass;
local hint;
col(colors.green);
term.setCursorPos(0,1);
screenClear();
print("PLEASE TYPE YOUR DESIRED USERNAME:");
usrn = read();
print("PLEASE TYPE YOUR DESIRED PASSWORD:");
pass = read("*");
print("PLEASE TYPE YOUR PASSWORD HINT:");
hint = read();
if not fs.isDir("/.winMC/.user/"..usrn) then
  fs.makeDir("/.winMC/.user/"..usrn);
  local usw = fs.open("/.winMC/.user/"..usrn.."/.userInfo","w");
  usw.writeLine(usrn);
  usw.writeLine(pass);
  usw.writeLine(hint);
  usw.close();
  if not fs.exists("/.winMC/.OS/hasUser") then
   fs.open("/.winMC/.OS/hasUser","w");
  end
else
  print("ERROR, USER ALREADY EXISTS, CHANGE USERNAME");
end
end
function login()
if not fs.exists("/.winMC/.OS/hasUser") then
createProfile();
local user = fs.open("/.winMC/.OS/hasUser","w");
user.close();
end
local usn;
local pass;
local tusn;
local tpass;
local uir;
local sucess = false;
while not sucess do
screenClear();
term.setCursorPos(1,1);
col(colors.green);
print("LOGIN");
print("USERNAME:");
term.setCursorPos(1,3);
print("PASSWORD:");
term.setCursorPos(10,2);
usn = read();
term.setCursorPos(10,3);
pass = read();
if fs.isDir("/.winMC/.user/"..usn) then
if fs.exists("/.winMC/.user/"..usn.."/.userInfo") then
  uir = fs.open("/.winMC/.user/"..usn.."/.userInfo","r");   tusn = uir.readLine();
  tpass = uir.readLine();
  if tusn == usn and tpass == pass then
	print("LOGIN SUCESS");
	user=usn;
	userDir = "/.winMC/.user/"..usn
	sucess = true;
  else
	col(colors.red);
	print("INCORRECT USERNAME OR PASSWORD");
  end
  uir.close();
else
  col(colors.red);
  print("ERROR, USER FOLDER EXISTS, BUT USER INFO FILE DOES NOT");
end
else
col(colors.red);
print("INCORRECT USERNAME OR PASSWORD");
end
sleep(1);
end
end
function screenClear()
term.clear();
bgc(colors.lightBlue);
for i=1,w do
  for j=1,h do
   term.setCursorPos(i,j);
   write(" ");
  end
end
end
function startScreen()
col(colors.green);
bgc(colors.lightBlue);
for i=1,#logo do
  term.setCursorPos(w/2 - 3,i);
  term.write(logo[i]);
  term.setCursorPos(w/2 - 5,4);
  term.write("LOADING ...");
  end
sleep(1);
end
function drawDesktop()
term.clear();
local desktopBG = paintutils.loadImage("/.winMC/.OS/desktopBG");
paintutils.drawImage(desktopBG,0,2);
term.setCursorPos(1,h);
bgc(colors.green);
col(colors.black);
write("START");
end
screenClear();
startScreen();
screenClear();
checkOsReq();
--sleep(2);
--createProfile();
login();
drawDesktop();
sleep(2);
PixelToast #2
Posted 22 May 2013 - 11:45 PM
/.OS/ dosent exist
crazyadam0 #3
Posted 23 May 2013 - 01:24 AM
AHA so the error is when the function is called. .OS is not supposed to exist,until the os downloads the program to create it along with other files and folders from pastebin. Oh wait. I see. It tries to download the checker to the folder the checker creates before the folder exists. FACEPALM. I will see if this fixes it in the morning. OFF TO BED!
SuicidalSTDz #4
Posted 23 May 2013 - 07:37 AM
For future reference, you do not need a semi colon after every line. Though the choice is yours, there is no negative effect by doing so.
crazyadam0 #5
Posted 23 May 2013 - 09:15 AM
Yeah, it's just that i grew up programming in C, and recently have been working in a C-syntax Javas like thing called Processing. So i usually chose to end each line with a ';' But thanks, i did not know it was optional. OH and i tested out the code this morning, it saves the checkOSR file in a temp directory, then once the /.OS directory has been made, it moves it there.