Jump to content

Creating, editing and reading files

help command api

2 replies to this topic

#1 _mika99_

  • Members
  • 11 posts
  • LocationAustria

Posted 05 September 2015 - 07:10 PM

Soo... I'm currently working on an OS called "Wundows". I want to start it with the common Windows startup screen, where you log in to the computer. I also want to have the OS support multiple users. My idea was, that I write Username and Password into a file and read it from there in the login-screen. The problem is, that I'm very new to the ComputerCraft Filesystem and even Tutorials didn't get me anywhere. >_< So basically what I'm looking for is a "very" Noob-Friendly Introduction to the filesystem. :)


#2 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 05 September 2015 - 09:43 PM

This might be a fun project, but it isn't a very good introduction to Lua.
Making a good CC OS requires a lot of advanced Lua knowledge.

That being said, you'll be using the FS api in a ton of stuff, so its good to know how it works:
(CC Wiki page: http://www.computerc...fo/wiki/Fs_(API) )
(More specifically, fs.open Wiki Page: http://www.computerc...fo/wiki/Fs.open )

To write text to a file:
local f = fs.open( "path" , "w" ) --# Opens the file called 'path', stores the handle in 'f'. 'w' means write-mode. See the wiki for all the modes

f.writeLine( "This is a line" ) --# Writes a line to the file

f.write("This is also a line\n") --# The \n is the newline character. writeLine adds it automatically

f.close() --# Saves the file ('w' mode will also create the file if it didn't exist)

Reading a file:
local f = fs.open("path","r") --# 'r' is read-mode

print( f.readLine() ) --# Reads first full line
--# Output: This is a line

local line = f.readLine() --# Reads the next line in the file
print( line )
--# Output: This is also a line

print( f.readLine() ) --# Read the next line
--# Output: Nothing! We've hit the end of the file, so it will now only return nil.

f.close() --# ALWAYS close file handles. If you don't, Java won't let other programs modify it
'f.readAll()' is also a thing, it just returns the entire file in one string. It includes the \n character, unlike 'readLine'

Hope this helps :)

Edited by HPWebcamAble, 05 September 2015 - 09:45 PM.

#3 _mika99_

  • Members
  • 11 posts
  • LocationAustria

Posted 06 September 2015 - 02:09 AM

This looks promising! I'm gonna try it tomorrow (today actually), because it's pretty late already. :D/> And you said that making a good OS requires much advanced LUA knowledge. I know that, and I want to get that knowledge in the process of making the OS. Also, I have some experiences in other programming languages such as Java, JavaScript, Visual Basic and C, so I think learning LUA will be abit easier for me. :)/>

Thanks for the nice "Introduction" to the fs API though :D/>

Edited by _mika99_, 06 September 2015 - 02:10 AM.

1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users