Jump to content




How do i interact with files in a program?


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

#1 oxygencraft

  • Members
  • 38 posts

Posted 17 November 2015 - 05:04 AM

What i am trying to do is make an API and i want to make a username and password function so the user needs to enter a username and password to exit to the terminal or something else. Each user will be stored in a file and its the same with the password BUT the passwords is hashed, the passwords are stored in a different file.

#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 17 November 2015 - 05:34 AM

fs API

#3 oxygencraft

  • Members
  • 38 posts

Posted 17 November 2015 - 09:03 AM

View PostKingofGamesYami, on 17 November 2015 - 05:34 AM, said:


Well i looked there before i started asking for help and i cant find a function to read/write text to files.

#4 Dragon53535

  • Members
  • 973 posts
  • LocationIn the Matrix

Posted 17 November 2015 - 09:49 AM

Tutorial

#5 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 17 November 2015 - 12:28 PM

to read/write text from/to a file you have to get a file handle:

local handle = fs.open(path,"w") -- "w" is for write
local handle = fs.open(path,"r") -- "r" is for read
local handle = fs.open(path,"a") -- "a" is for append

Then depending on what you want to do you use different functions:

Read:
handle.readAll()
Write:
handle.write(data)
Append:
handle.write(data)

Take not that when you open a file using "w", you cannot read from it.

Edited by Creator, 17 November 2015 - 12:28 PM.


#6 Twijn

  • Members
  • 119 posts

Posted 18 November 2015 - 10:25 PM

FS API, fs.open

That should be all you need for basic file manipulating. :)

Edited by Twijn, 18 November 2015 - 10:25 PM.


#7 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 18 November 2015 - 11:21 PM

View PostTwijn, on 18 November 2015 - 10:25 PM, said:

FS API, fs.open

That should be all you need for basic file manipulating. :)
Don't forget fs.close - always close your files when you're done with them.

#8 Twijn

  • Members
  • 119 posts

Posted 19 November 2015 - 02:09 AM

View PostDog, on 18 November 2015 - 11:21 PM, said:

View PostTwijn, on 18 November 2015 - 10:25 PM, said:

FS API, fs.open

That should be all you need for basic file manipulating. :)
Don't forget fs.close - always close your files when you're done with them.

h.close is not a part of the FS api, it is a part of what is returned from fs.open (which is a table, basically), and it's also listed in the fs.open link above, however it is important and worth noting that you need to close the files as well. :)

Also, if you accidentally forget to close it (which I have done before) then it's fairly simple to fix it. Simply restart the computer. I'm not positive on whether or not the changes you made to it are kept when you don't close it. If you forget to close it you will often quickly realize because you cannot delete the file or even rename it or open it again as far as I know.

Edited by Twijn, 19 November 2015 - 02:15 AM.


#9 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 19 November 2015 - 02:36 AM

View PostTwijn, on 19 November 2015 - 02:09 AM, said:

View PostDog, on 18 November 2015 - 11:21 PM, said:

View PostTwijn, on 18 November 2015 - 10:25 PM, said:

FS API, fs.open

That should be all you need for basic file manipulating. :)
Don't forget fs.close - always close your files when you're done with them.

h.close is not a part of the FS api, it is a part of what is returned from fs.open (which is a table, basically), and it's also listed in the fs.open link above, however it is important and worth noting that you need to close the files as well. :)

Also, if you accidentally forget to close it (which I have done before) then it's fairly simple to fix it. Simply restart the computer. I'm not positive on whether or not the changes you made to it are kept when you don't close it. If you forget to close it you will often quickly realize because you cannot delete the file or even rename it or open it again as far as I know.
Yeah, I stated that rather poorly - thanks for correcting and clarifying that :)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users