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

Easiest Way To Read Data In A File

Started by jay5476, 23 August 2013 - 02:23 AM
jay5476 #1
Posted 23 August 2013 - 04:23 AM
what is the easiest way to insert data from a file after every newline eg.

local table = {}
--[[ file looks like
file
other
test
have a function read that and insert into a table like]]--
table = {"file","other","test"} -- this is what table should be
thanks for help in advanced
theoriginalbit #2
Posted 23 August 2013 - 04:29 AM
it is quite easy to do this, there are several approaches that people take. This is the approach that I have been taking lately:


--# the lines in the file
local fileData = {}

--# open the file
local handle = assert(fs.open("some_file", 'r'), "Cannot open file for read")

--# read all the lines from the file using an iterator
for line in handle.readLine do
  --# insert the line into the table
  table.insert(fileData, line)
end

--# close the file header
handle.close()

The above is one of the easiest methods.
jay5476 #3
Posted 23 August 2013 - 04:43 AM
derpy me forgot about readLine bean using readAll lately
theoriginalbit #4
Posted 23 August 2013 - 05:09 AM
Haha, it's ok, we all have those moments from time to time.