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

Creating, editing and reading files

Started by _mika99_, 05 September 2015 - 06:10 PM
_mika99_ #1
Posted 05 September 2015 - 08: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. :)/>

Thanks,
Mika
HPWebcamAble #2
Posted 05 September 2015 - 10: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.computercraft.info/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 on 05 September 2015 - 08:45 PM
_mika99_ #3
Posted 06 September 2015 - 03: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 on 06 September 2015 - 01:10 AM