Jump to content




Fs (API), what is that?


28 replies to this topic

#1 Strite

  • Members
  • 66 posts
  • LocationBrazil

Posted 29 November 2013 - 05:30 PM

Heyo everybody, this is probably a very stupid question, but I really want to know what the 'Fs API' is used for.

I've seen it in a couple Programs out there, but I don't really understand it - I saw the wiki, but nothing there really helped me, I'm just looking for a quick and easy explanation, and, if possible, some examples.

Thanks.

#2 amtra5

  • Members
  • 166 posts
  • LocationMelbourne, Australia

Posted 29 November 2013 - 06:15 PM

The FS API is used for reading and writing to the file system of that computer. Of course, you could also use the IO API, but that is just a wrapper for FS.


#3 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 29 November 2013 - 06:26 PM

It's not just a wrapper for the FS API, it also adds the :lines() method.

#4 Strite

  • Members
  • 66 posts
  • LocationBrazil

Posted 29 November 2013 - 06:30 PM

I don't really know what files are, in terms of cc - basicly, are files equals programs? If not, what are they, exactly?

#5 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 29 November 2013 - 06:38 PM

Files are just files; what they are and what they're used for in the context of CC is the same as any real life computer. In CC, files can be run as programs, but they're still just files.

#6 Strite

  • Members
  • 66 posts
  • LocationBrazil

Posted 29 November 2013 - 09:24 PM

So, they can theoretically be used to do pretty much anything? Like, store information, start programs, print, and etc?

Edited by Strite, 29 November 2013 - 09:24 PM.


#7 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 29 November 2013 - 09:38 PM

Yes, and they are used that way quite often.

#8 Strite

  • Members
  • 66 posts
  • LocationBrazil

Posted 29 November 2013 - 09:45 PM

I just don't see the advantage of using a file over a program itself, I'm probably just being stupid but, if a program can do everything a file can, why to use a file, then?

EDIT: dat rhyme.

Edited by Strite, 29 November 2013 - 09:45 PM.


#9 MudkipTheEpic

  • Members
  • 639 posts
  • LocationWhere you'd least expect it.

Posted 29 November 2013 - 10:30 PM

View PostStrite, on 29 November 2013 - 09:45 PM, said:

I just don't see the advantage of using a file over a program itself, I'm probably just being stupid but, if a program can do everything a file can, why to use a file, then?

EDIT: dat rhyme.

A program is a file that can be run. Files and directories (or folders) are part of a file structure. Directories CONTAIN files.

Files are where data is stored. Pictures, images, programs, plain text, even applications are types of files. The FS API is an API used for editing or reading files in their plaintext or binary forms, or also for creating directories, and moving/deleting/copying files/directories.

TL;DR: Go back and take a minute of your time to read what I said.

#10 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 29 November 2013 - 11:55 PM

View PostStrite, on 29 November 2013 - 09:45 PM, said:

I just don't see the advantage of using a file over a program itself, I'm probably just being stupid but, if a program can do everything a file can, why to use a file, then?

EDIT: dat rhyme.
It'd be pretty difficult for a program to store its data in the program itself, wouldn't you think? The program would need to use a separate, non-program file for that.

#11 Strite

  • Members
  • 66 posts
  • LocationBrazil

Posted 30 November 2013 - 12:30 AM

I'm kinda getting it now, but, in tearms of computercraft, what can we do with it? If possible, give me some simple examples, so I can see it better. It's kinda hard to visualize it this way - Sorry for the extreme newbie behavior.

#12 jay5476

  • Members
  • 289 posts

Posted 30 November 2013 - 12:55 AM

Well most commonly people use it with their turtle program's to keep track of where it is in case of a shutdown and having to start from the start( cant give some code here )
But an example i can give is a password lock
repeat
if fs.exists("password") then
  fileHandle = fs.open("password","r") -- open file in read mode
  local pass = fileHandle.readAll() -- read everything in the file
  fileHandle.close() -- make sure to close
else
  write("Enter new password: ")
  local pass = read("*")
  fileHandle = fs.open("password","w") -- open file in write mode
  fileHandle.write(pass) -- store password in the file
  fileHandle.close() -- again close the file
end
term.clear()
term.setCursorPos(1,1)
write("Enter Your Password: ")
input = read("*") 
until input == pass

Edit: Basically if a password file doesn't exist yet then it will create the file and write the password to it otherwise it will read from the password file and then store it in the variable called pass and then compare later on

Edited by jay5476, 30 November 2013 - 12:57 AM.


#13 Strite

  • Members
  • 66 posts
  • LocationBrazil

Posted 30 November 2013 - 01:03 AM

It's still a bit confusing to me, I'm just stupid, don't mind me - I'm going to search a little more, do some reseach, some testing, I'll figure it out.
But still, thank you guys, this whole topic is much more clear now, at least know I know the very basics, you really helped me!

See ya' all later!

Edited by Strite, 30 November 2013 - 01:04 AM.


#14 jay5476

  • Members
  • 289 posts

Posted 30 November 2013 - 01:41 AM

I know your a bit confused but, files arnt just for program's. See above posts but files can be a useful way to save information that you can then read or edit from th fs api

#15 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 30 November 2013 - 02:35 AM

View PostStrite, on 29 November 2013 - 06:30 PM, said:

I don't really know what files are, in terms of cc - basicly, are files equals programs? If not, what are they, exactly?
I suspect you've somehow got the mindset that "CC files" are different to "regular files" that you may have on your own real life computer.

They're not. When you use the FS API to open a file and write data into it, it's creating a real file on the server and writing data into that. The whole MineCraft server can be shut down and restarted and your file will stay there, ready to read from when you want it. On the other hand, the contents of your CC computer's "RAM" gets cleared as soon as the chunk they're in unloads (same as what happens to the RAM of your real computer if you turn THAT off).

#16 Strite

  • Members
  • 66 posts
  • LocationBrazil

Posted 17 December 2013 - 08:58 PM

I once again tried to learn a bit more about Fs, after this couple weeks, and I started by, whitout looking at this topic, trying to create a password system. The code "works", at first it asks for the new password, I typed "123456", without the brackets of course, and next, the program asked me to retype the password, I did, but the program seems to understand that the password is wrong. It keeps asking me the password, although I already did.
Here's the code: pastebin.com/4L8tLppc
What am I doing wrong?

#17 awsmazinggenius

  • Members
  • 930 posts
  • LocationCanada

Posted 17 December 2013 - 11:06 PM

There are some things wrong before I will even go into your specific code. First, use an if statement to check passwords in a while loop. You can implement a timeout after too many failures, and when input == password you can use the break keyword. In your code, your input is local to the repeat body, so the check always fails. (I think, don't quote me)

#18 Patric20878

  • Members
  • 13 posts

Posted 18 December 2013 - 07:30 AM

awsmazinggenius is correct, and your repeat loop is fine. There's no need to use an if statement in a while loop here. The variable, pass, is local, and so only exists on the same level it's declared as well as deeper levels, which in this case is inside the repeat body. For example, a series of conditional statements would have scope that looks like:

--start level 1
repeat
  --start level 2

  if x == 1 then
	--start level 3
	print("asdf")
	--end level 3
  end

  --end level 2
until y == 1

--end level 1

You can fix your code by declaring pass before the repeat loop with say, local pass = "", then inside the if statement, change that to pass = file.readAll()

This is an example of block scope in programming. Read about it in http://en.wikipedia....mputer_science)

Edited by Patric20878, 18 December 2013 - 07:45 AM.


#19 awsmazinggenius

  • Members
  • 930 posts
  • LocationCanada

Posted 18 December 2013 - 09:35 AM

Part of the reason indentation is useful - lets you see the scope of variables. You could make pass global, but that is not a good choice because any program can print(pass) to see the password.

While you are at it, I would encrypt or hash the password on disk. I use GravityScore's SHA256 Algorithm to hash passwords on disk, and then I hash the input as well in awsmazingOS.

Edited by awsmazinggenius, 18 December 2013 - 09:36 AM.


#20 Strite

  • Members
  • 66 posts
  • LocationBrazil

Posted 18 December 2013 - 02:07 PM

Thanks for all the suggestions and the corrections, it was really helpful. But, the point here isn't actually to code a flawless password system that no one can acess, if I wanted to do that, I would actually focused on that, probably with the help of you guys. I was just testing the fs API, which I'm absolutely terrible at. I figured the problem with the variables and the files, so the problem was solved, and I really am understading a bit more about the API as a whole now. But, awsmazinggenius, you said something quite interesting that I never heard about. What is this GravityScore's SHA256 Algorithm that you talked about? I'm curious.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users