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

Fs problems...

Started by SethShadowrider, 26 January 2013 - 09:10 PM
SethShadowrider #1
Posted 26 January 2013 - 10:10 PM
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()
Zoinky #2
Posted 26 January 2013 - 10:17 PM
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 on 26 January 2013 - 09:21 PM
SethShadowrider #3
Posted 27 January 2013 - 10:48 AM
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?
crazyguymgd #4
Posted 27 January 2013 - 11:05 AM
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
remiX #5
Posted 27 January 2013 - 11:09 AM
local spName = sNAme.readLine()

sNAme must be sName

edit: had tab open for too long xD
SethShadowrider #6
Posted 27 January 2013 - 01:18 PM
That was actually an error in rewriting it into the browser i'm playing multiplayer.
Zoinky #7
Posted 27 January 2013 - 03:27 PM
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.
crazyguymgd #8
Posted 27 January 2013 - 05:38 PM
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 :)/>
SethShadowrider #9
Posted 28 January 2013 - 09:55 AM
Oooh okay thanks I assumed that a would let me read as well as write.
Thanks!