Jump to content




File Needlessly Being Cleared


  • You cannot reply to this topic
8 replies to this topic

#1 applesauce10189

  • Members
  • 162 posts

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

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 by applesauce10189, 05 March 2014 - 06:15 AM.


#2 CometWolf

  • Members
  • 1,283 posts

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...

#3 applesauce10189

  • Members
  • 162 posts

Posted 05 March 2014 - 06:16 AM

View PostCometWolf, on 05 March 2014 - 06:11 AM, said:

"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 by applesauce10189, 05 March 2014 - 06:19 AM.


#4 CometWolf

  • Members
  • 1,283 posts

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()


#5 applesauce10189

  • Members
  • 162 posts

Posted 05 March 2014 - 09:02 AM

View PostCometWolf, on 05 March 2014 - 07:34 AM, said:

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

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 by applesauce10189, 05 March 2014 - 09:41 AM.


#6 CometWolf

  • Members
  • 1,283 posts

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 by CometWolf, 05 March 2014 - 10:02 AM.


#7 applesauce10189

  • Members
  • 162 posts

Posted 05 March 2014 - 10:03 AM

View PostCometWolf, on 05 March 2014 - 09:58 AM, said:

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 by applesauce10189, 05 March 2014 - 10:31 AM.


#8 CometWolf

  • Members
  • 1,283 posts

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.

#9 applesauce10189

  • Members
  • 162 posts

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!





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users