Posted 05 March 2014 - 05:59 AM
I'm making a program that uses various files with the fs API and I use the same thing to create a file if the file doesn't yet exist, I used it on about 3-4 spots in my code for different files and for some reason there's only 1 that for some reason gets passed the if not …… then thing. Here's an example of what I'm doing.
That's worked everywhere except on one spot.
Here's the problematic area of code.
Later on I have a first time start up function so you can define the variable secret but after it's defined the file is still getting wiped in that if statement even though the file admin already exists. Even then, there's also a secret option in the menu function which allows you to re-define the variable, both of these ways to define the variable get overwritten because somehow it thinks the admin file doesn't exist.
EDIT: I have a lot of code so if you think it would help to look at the entire thing, here's a spoiler
EDIT#2: One more problem, even though aCheck is set to "yes" it skips the first time start up that it's supposed to go through if aCheck says "yes"
if not fs.exists("rom/fileName") then
local file = fs.open("fileName", "w")
-- set defaults for file if any.
file.close()
end
That's worked everywhere except on one spot.
Here's the problematic area of code.
if not fs.exists("rom/admin") then
local file = fs.open("admin", "w")
file.close()
end
local file = fs.open("admin", "r")
local secret = file.readLine()
file.close()
Later on I have a first time start up function so you can define the variable secret but after it's defined the file is still getting wiped in that if statement even though the file admin already exists. Even then, there's also a secret option in the menu function which allows you to re-define the variable, both of these ways to define the variable get overwritten because somehow it thinks the admin file doesn't exist.
EDIT: I have a lot of code so if you think it would help to look at the entire thing, here's a spoiler
Spoiler
--files list. best, admin, aCheck.
if not fs.exists("rom/aCheck") then
local file = fs.open("aCheck", "w")
file.writeLine("yes")
file.close()
end
function firstTime()
if not fs.exists("rom/apple") then
shell.run("pastebin get G67vP0VE apple")
end
os.unloadAPI("apple")
os.loadAPI("apple")
if check == "yes" then
apple.reset()
print("Welcome to first time start up for the Apple Arcade.")
print("Please insert a password for config access.")
secret = read("*")
print("If for some reason you want to change this")
print("in the future, type 'apples secret'")
local file = fs.open("aCheck", "w")
file.writeLine("no")
file.close()
return
else
return
end
end
if not fs.exists("rom/admin") then
local file = fs.open("admin", "w")
file.close()
end
local file = fs.open("admin", "r")
local secret = file.readLine()
file.close()
print("secret = "..secret)
local file = fs.open("aCheck", "r")
local check = file.readLine()
file.close()
function menu()
firstTime()
if not fs.exists("rom/apple") then
shell.run("pastebin get G67vP0VE apple")
end
os.unloadAPI("apple")
os.loadAPI("apple")
apple.reset()
print("Welcome to Apple's arcade! This is a heavy")
print("work in progress so many things may break,")
print("Here are your options.")
print("1. Guess that number! Type 'number'")
print("2. Edit/reset configs. Type the password.")
print("There's more to come.")
local select = read()
while true do
if select == "number" then
number()
elseif select == "apples secret" then
print("Hello, please insert config password.")
local file = fs.open("admin", "w")
select = read()
file.writeLine(select)
file.close()
menu()
elseif select == secret then
print("Which config would you like to change or reset?")
print("There's best for number,")
print("In the future there will be more.")
select = read()
if select == "change best" then
print("What should it be changed to?")
select = read()
select = tonumber(select)
local file = fs.open("best", "w")
file.writeLine(select)
file.close()
end
elseif select == "reset best" then
local reset = 1000
local file = fs.open("best", "w")
file.writeLine(reset)
file.close()
print("done.")
elseif select == "change pass" then
print("what should the password be?")
select = read()
local file = fs.open("admin", "w")
file.writeLine(select)
file.close()
print("done.")
else
print("Sorry, but "..select.." isn't an option.")
select = read()
end
end
end
function number()
if not fs.exists("rom/apple") then
shell.run("pastebin get G67vP0VE apple")
end
os.unloadAPI("apple")
os.loadAPI("apple")
apple.reset()
print("Thanks for playing guess that number!")
local file = fs.open("best", "r")
best = file.readLine()
file.close()
print("Would you like to use a custom range?")
print("(If you say no it will be 1-100)")
print("The current best is: "..best)
print("? ")
term.setCursorPos(3,5)
local tries = 0
if not tonumber(best) then
local best = 100
local file = fs.open("best", "w")
file.write(best)
print(best)
file.close()
end
while true do
local answer = read()
if answer == "yes" then
print("Sorry this feature is a work in progress,")
print("Please enter minimum then maximum")
local min = read()
local max = read()
break
elseif answer == "no" then
print("Okay.")
local min = 1
local max = 100
break
else
print("Pease say 'yes' or 'no'")
end
end
local correct = math.random(1, 100)
while true do
local guess = read()
guess = tonumber(guess)
tries = tries+1
if guess > correct then
print("Too high!")
elseif guess < correct then
print("Too low!")
elseif guess == correct then
print("juuussst riiight")
print(tries)
print(best)
if tonumber(tries)<tonumber(best) then
best = tries
print(best)
local file = fs.open("best", "w")
file.write(best)
file.close()
end
break
else
print("Sorry, there was a problem.")
return
end
end
end
menu()
EDIT#2: One more problem, even though aCheck is set to "yes" it skips the first time start up that it's supposed to go through if aCheck says "yes"
Edited on 05 March 2014 - 05:15 AM