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

File Needlessly Being Cleared

Started by applesauce10189, 05 March 2014 - 04:59 AM
applesauce10189 #1
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.


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
CometWolf #2
Posted 05 March 2014 - 06:11 AM
"rom/fileName" and "/fileName" is obviously not the same thing. You can't even put files in the rom folder…
applesauce10189 #3
Posted 05 March 2014 - 06:16 AM
"rom/fileName" and "/fileName" is obviously not the same thing. You can't even put files in the rom folder…
So remove the rom from each of the fs.exists? I'm new to that part of the fs API, I'm only using it because I just recently found out it even exists.
EDIT: Removing rom didn't fix it. It still keeps getting cleared.
Edited on 05 March 2014 - 05:19 AM
CometWolf #4
Posted 05 March 2014 - 07:34 AM
So now it looks like this?

if not fs.exists("admin") then
  local file = fs.open("admin", "w")
  file.close()
end
local file = fs.open("admin", "r")
local secret = file.readLine()
file.close()
applesauce10189 #5
Posted 05 March 2014 - 09:02 AM
So now it looks like this?

if not fs.exists("admin") then
  local file = fs.open("admin", "w")
  file.close()
end
local file = fs.open("admin", "r")
local secret = file.readLine()
file.close()
Should it? I only got rid of the rom part, guess I should've done the same for the /'s,

EDIT: Removed the /'s and both the check function and the admin file aren't working as they should.

EDIT#2: 123 posts :P/> totally not childish.

EDIT#3: Thinking about it, I've made a couple changes to the code so I should probably put the updated code…. Quick note, right now my problems are
1. The admin file is either being overwritten or something is causing the "secret" variable to be nil.
2. the function firstTime isn't being set off even though it should always run the firstTime function until it's been run at least once.

EDIT#4: I think it would help you read my code if I were to remember to put it in.

Spoiler

--files list. best, admin, aCheck.

if not fs.exists("aCheck") then
  local file = fs.open("aCheck", "w")
  file.writeLine("yes")
  file.close()
end

function firstTime()
  if not fs.exists("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("admin") then
  local file = fs.open("admin", "w")
  file.close()
end
local file = fs.open("admin", "r")
local secret = file.readLine()
file.close()


local file = fs.open("aCheck", "r")
local check = file.readLine()
file.close()


function menu()

firstTime()

if not fs.exists("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("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#5: NOT ENOUGH EDITS!!! jk, The problem with problem 1 is both the admin file is being overwritten to blank AND the variable never gets defined.

EDIT#6: It's late my time and replies are taking a bit to get here so I'm assuming it's late for others, while I await answers to my questions I shall make a makeshift password door with changeable passwords in creative with a little house with various things to control from one computer using a rednet network.
Edited on 05 March 2014 - 08:41 AM
CometWolf #6
Posted 05 March 2014 - 09:58 AM
Im at work so i can only respond in my downtime :P/>/> the slashes don't matter, but i prefer having them there myself. Lol at the function name! The check variable in the function points to the global variable, while you read the file into the local variable. Moving the function definition below the part you define the variable locally would fix it. What you're saying about secret is correct.
Edited on 05 March 2014 - 09:02 AM
applesauce10189 #7
Posted 05 March 2014 - 10:03 AM
Im at work so i can only respond in my downtime :P/>/> the slashes don't matter, but i prefer having them there myself. Lol at the function name! The check variable in the function points to the global variable, while you read the file into the local variable. Moving the function definition below the part you define the variable locally would fix it. What you're saying about secret is correct.
I actually call firstTime at the beginning of the menu function

EDIT: I'm a little lost, could you give me the line I made my mistake on?

EDIT#2: Sorry for taking so long to get the first edit, had to go do something so I slept my laptop, it was quicker than I thought so I ended up having to wait for my laptop to wake up and load some stuffs.
Edited on 05 March 2014 - 09:31 AM
CometWolf #8
Posted 05 March 2014 - 10:49 AM
Move the code where you define the function firstTime below this part of the code.

local file = fs.open("aCheck", "r")
local check = file.readLine()
file.close()
The issue here is that you try to use the check variable prior to defining it locally, and then define it locally. So they don't point to the same variable.
applesauce10189 #9
Posted 05 March 2014 - 11:04 AM
Thanks for the help! As far as I can tell everything seems to be working fine now. Now to add games to it and fix the 20 million bugs that come up from that!