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

File options (Need help)

Started by gheotic, 23 January 2013 - 04:59 AM
gheotic #1
Posted 23 January 2013 - 05:59 AM
Im trying to make a email system that stores the email in a file

but i have some questions:

How do i change what line it write on?
why dosnt it write but just create the file when i want it to print on a disk?
how do i make it read from a line?



any help would be appreciated =)
FuuuAInfiniteLoop(F.A.I.L) #2
Posted 23 January 2013 - 06:15 AM
file = fs.open("file", "w")
file.write() — write a line to the file and go to the next line

file = fs.open("file, "r")
file.read() — read all the file
file.readLine() — reads the next line of the file and go to the next line
remiX #3
Posted 23 January 2013 - 08:12 AM
remember to close the file handles!

file.close()
gheotic #4
Posted 23 January 2013 - 07:50 PM
so if i want to make my program write emails down to the file on each line

how do i chose which line it should read?
theoriginalbit #5
Posted 23 January 2013 - 07:58 PM
file.write() — write a line to the file and go to the next line
Actually it just writes the string, you need to insert a new line yourself with file.write( someVar.."\n )

file.read() — read all the file
Its actually file.readAll() to read the entire file… there is actually no read in the fs api, if you want read its from the io api.

how do i chose which line it should read?
Sadly there is no easy way that is implemented in ComputerCraft… normally you would use file.seek() however this was not implemented so you need to read each line into a table and then get the line from the table. ( NOTE: with large files this can be quite costly doing this each time, if you plan on having large files consider maybe reading the file into a table at the start and only rereading it at a certain interval or case )
Edited on 23 January 2013 - 07:01 PM
remiX #6
Posted 23 January 2013 - 10:04 PM
Or you could use file.writeLine("text") which inserts a new line automatically