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

[help] help needed.(fs.open)

Started by Goof, 10 October 2012 - 10:07 AM
Goof #1
Posted 10 October 2012 - 12:07 PM
Hey!

i am trying to make a program, that makes the directory "%appdata%", and then edit a file called "saves.daT", but!
the first time i started the program it works fine. but the second time (when the "saves.daT" already exists, it does not show the "Lollypop1" sign.)


the code is here..



function makeDir()
fs.makeDir("%appdata%")
if not fs.exists("%appdata%/saves.daT") then     -- when this path dosent exist, it works fine, but... --
h = fs.open("%appdata%/saves.daT", "w")
h.write("Lollypop")
h.close()
elseif fs.exists("%appdata%/saves.daT") then    -- when the path Does exists, it does not read the "%appdata%/saves.daT" file.... Why?
h.readAll("Lollypop1")
h.close()
end
end


makeDir()






- Mikk809h



if anyone could help me, i will be very happy….


Thanks
nolongerexistant #2
Posted 10 October 2012 - 01:12 PM
h.readAll("Lollypop1")

That doesn't work I believe

Try this:

function makeDir()
    if not fs.exists("%appdata%") then -- Check if %appata% exists first
	    fs.makeDir("%appdata%")
    end
    if not fs.exists("%appdata%/saves.daT") then
		h = fs.open("%appdata%/saves.daT", "w")
		h.write("Lollypop")
		h.close()
	elseif fs.exists("%appdata%/saves.daT") then
		lolypop1 = h.readAll() -- Make the readAll a variable to use it later on
		print(lolypop1) -- Print's all the text of the file on screen, you can do anything with it.
		h.close()
	end
end

makeDir()

Also indent your code :P/>/>
Goof #3
Posted 10 October 2012 - 01:30 PM
Oh. Thanks for the quick answer. I will try the code, and will post back, if ther is any errors. :-)

-thanks
remiX #4
Posted 10 October 2012 - 01:34 PM
Also indent your code :P/>/>

Why don't people ever do this ;)/>/>
Goof #5
Posted 10 October 2012 - 01:38 PM
It says: ex:10: attempt to call nil. Code is the same as yours….


the line 10 : Lol1 = h.readAll()




edit: i've found the error. But thanks anyway
Cozzimoto #6
Posted 10 October 2012 - 01:47 PM
try creating a while loop to read line by line until it reaches a nil, then break the loop and place them all in a table and access you data with table indexes
Doyle3694 #7
Posted 10 October 2012 - 08:43 PM
There is a fs tut in the tutorials section. check it out :P/>/>