Jump to content




[help] help needed.(fs.open)

api help lua

6 replies to this topic

#1 Goof

  • Members
  • 751 posts

Posted 10 October 2012 - 11:07 AM

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

#2 nolongerexistant

  • Validating
  • 201 posts
  • LocationNetherlands

Posted 10 October 2012 - 12: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/>

#3 Goof

  • Members
  • 751 posts

Posted 10 October 2012 - 12:30 PM

Oh. Thanks for the quick answer. I will try the code, and will post back, if ther is any errors. :-)

-thanks

#4 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 10 October 2012 - 12:34 PM

View PostSnakybo, on 10 October 2012 - 12:12 PM, said:

Also indent your code :P/>

Why don't people ever do this ;)/>

#5 Goof

  • Members
  • 751 posts

Posted 10 October 2012 - 12: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

#6 Cozzimoto

  • Members
  • 221 posts
  • LocationDallas, Tx

Posted 10 October 2012 - 12: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

#7 Doyle3694

  • Members
  • 815 posts

Posted 10 October 2012 - 07:43 PM

There is a fs tut in the tutorials section. check it out :P/>





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users