173 posts
Location
The hall of 1000 monkeys and only 1 typewriter
Posted 30 November 2015 - 07:13 PM
hia! I have the need to make a system that stores the first input that the program ever recieves (including the time when the program was launched before) and refers to it later.
I'm basically making my OS and i need help on the login XD.
so, so far i have managed to write things to a file, but now i need help looking at that file.
It would also be nice if you could supply me with detecting whether it exists or not! ik it has something to do with fs.exists but still.
I haven't really got used to fs so it would be nice if you explaind why things do other things
2679 posts
Location
You will never find me, muhahahahahaha
Posted 30 November 2015 - 07:28 PM
Existence:
fs.exists(path)
Read:
local file = fs.open(path,"r")
local data = file.readAll()
file.close()
350 posts
Posted 30 November 2015 - 07:29 PM
Hey! To check if a file exists do like this:
if fs.exists("filename") then
--Do stuff
end
To read a file you just do the same thing as if writing, but with r instead of w:
file = fs.open("file", "r")
content = file.readAll() --#read all the file
line = file.readLine() --#you can also read only one line at a time
file.close() --dont forget to close
The
fs api documentation is pretty easy to understand. Just spend a couple of minutes reading the snippets :)/>
2427 posts
Location
UK
Posted 30 November 2015 - 07:30 PM
your program should know what it called (or would call) the file when it made it last time (this should be hardcoded or a variable in your program), so don't look for it but do what you did to write the file, but open it in read mode
edit: :ph34r:/>
Edited on 30 November 2015 - 06:31 PM
350 posts
Posted 30 November 2015 - 07:38 PM
Double kill
173 posts
Location
The hall of 1000 monkeys and only 1 typewriter
Posted 01 December 2015 - 06:15 PM
k so how would i look at a specific line?
would i do something like file.readLine([1])?
350 posts
Posted 01 December 2015 - 06:20 PM
Actually that's more tricky.
Everytime you run file.readLine() it jumps a line and reads it. So you have to run it in a loop or make a function to return a specific line. Like a while loop that increments 1 in each loop, and when the increment variable is == to the line you want, then return the file.readLine()
8543 posts
Posted 01 December 2015 - 07:43 PM
The easiest way is to read all of the lines into a table, then index specific lines out of the table.
1220 posts
Location
Earth orbit
Posted 01 December 2015 - 07:49 PM
If your file isn't serialized data and you only need to read the file once (or every so often), you could do something like this…
local line = { }
if fs.exists(filename here in quotes) then --# if the file exists then do the following...
for lineEntry in io.lines(filename here in quotes) do --# read the file in, line by line
line[#line + 1] = lineEntry --# save each line as a new table entry
end
else --# if the file doesn't exist then do something else...
--# stuff to do if the file doesn't exist
end
This reads the entire file in and puts each line in a table entry
Then you can access any line using its line number like so…
term.write(line[1]) --# write the contents of the first line to the terminal screen
local lineFour = line[4] --# set the local variable lineFour to equal line 4 as read from the file
if password == line[2] then --# compare an entered password with the data read from line 2 of the file
--# and so on and so forth...
If the file changes regularly and needs to be repeatedly re-read then this will not be the most efficient way to go about getting the data you want. But if you're just reading it once per program launch or login session (and the file isn't very big) then this should be OK.