←  Ask a Pro

ComputerCraft | Programmable Computers for Minecraft

»

Fs problems...

SethShadowrider's Photo SethShadowrider 26 Jan 2013

So let me start off by making this statement. I am completely stumped. For a long time i've been trying different ways of writing this code, originally it gave me the error of attempted to index nil when attempting to open the file I was editing with the fs api. Since then its gone to telling me the stream ended when attempting to write to the file. I googled around and checked the help in computercraft for almost 2 hours straight and have come up with nothing so I am going to post it here. Sorry if it's a bit messy I haven't really refined it much yet i'm looking for functionality for now.

--get the screen size
local w,h = term.getSize()
--helpful functions
function printCentered(str, ypos)
term.setCursorPos(w/2 - #str/2, ypos)
term.write(str)
end
function printRight(str, ypos)
term.setCursorPos(w - #str, ypos)
term.write(str)
end
function poot()
while true do
sleep(.1)
local x = math.random(1, w)
local y = math.random(1, h-1)
term.setCursorPos(x, y)
print("Poot")
end
end
function drawLogo()
shell.run("clear")
term.setCursorPos(w/2-8, 2)
textutils.slowPrint("Fierce Deity Inc.")
term.setCursorPos(w/2-8, 3)
textutils.slowPrint("-----------------")
end

function newAcc()
if fs.exists("newAcc") == false then
fs.open("newAcc", "a")
drawLogo()
printCentered("New User", 4)
printCentered("--------", 5)
printCentered("Username:", 7)
printCentered("--------------", 9)
term.setCursorPos(w/2-7, 8)
local uname = read()
local file = fs.open("newAcc", "a")
file.close()
file.writeLine(uname)
elseif fs.exists("newAcc") == true then
shell.run("clear")
print("User exists!")
end
end
newAcc()
Quote

Zoinky's Photo Zoinky 26 Jan 2013

You're closing the program (Woops! :P) file before you're writing in it. I tried to fix your function up a little, see if this works:

function newAcc()
 if fs.exists("newAcc") == false then
  drawLogo()
  printCentered("New User", 4)
  printCentered("--------", 5)
  printCentered("Username:", 7)
  printCentered("--------------", 9)
  term.setCursorPos(w/2-7, 8)
  local uname = read()
  local file = fs.open("newAcc", "a")
  file.writeLine(uname)
  file.close()
 elseif fs.exists("newAcc") == true then
  term.clear()
  term.setCursorPos(1,1)
  print("User exists!")
 end
end

Edited by Zoinky, 26 January 2013 - 10:21 PM.
Quote

SethShadowrider's Photo SethShadowrider 27 Jan 2013

That worked great but now i'm back at square one. When I attempt to execute this code I get the error attempted to index nil
local sName = fs.open("newAcc", "a")
local spName = sNAme.readLine()
sName.close()
print("Welcome"..spName)
is there any fix for this?
Quote

crazyguymgd's Photo crazyguymgd 27 Jan 2013

View PostSethShadowrider, on 27 January 2013 - 10:48 AM, said:

That worked great but now i'm back at square one. When I attempt to execute this code I get the error attempted to index nil
local sName = fs.open("newAcc", "a")
local spName = sNAme.readLine()
sName.close()
print("Welcome"..spName)
is there any fix for this?

second line you have sNAme instead of sName
Quote

remiX's Photo remiX 27 Jan 2013

local spName = sNAme.readLine()

sNAme must be sName

edit: had tab open for too long xD
Quote

SethShadowrider's Photo SethShadowrider 27 Jan 2013

That was actually an error in rewriting it into the browser i'm playing multiplayer.
Quote

Zoinky's Photo Zoinky 27 Jan 2013

View PostSethShadowrider, on 27 January 2013 - 01:18 PM, said:

That was actually an error in rewriting it into the browser i'm playing multiplayer.
You need to set it into read mode to get the name from the file. Use this:
local sName = fs.open("newAcc", "r")
Instead of:
local sName = fs.open("newAcc", "a")

EDIT: For future reference,

A = Append mode. Use this to add lines to a file.
W = Write mode. Use this to rewrite the content of a file. (Write mode will delete everything already in the file and put in the lines you give it.)
R = Read mode. Use this to read from a file.

You can find more info and some examples about the FS API here.
Quote

crazyguymgd's Photo crazyguymgd 27 Jan 2013

View PostSethShadowrider, on 27 January 2013 - 01:18 PM, said:

That was actually an error in rewriting it into the browser i'm playing multiplayer.

Ahh well zoinky was right, use r instead of a. I always look for typos first and then assume that's the problem :)
Quote

SethShadowrider's Photo SethShadowrider 28 Jan 2013

Oooh okay thanks I assumed that a would let me read as well as write.
Thanks!
Quote