Jump to content




Reading and writing files

help

8 replies to this topic

#1 eastar

  • Members
  • 17 posts

Posted 29 June 2019 - 11:57 AM

Hello!

I don't know how to read from files effectively.
I have used the fh.readLine() function before, but since I didn't have much data to read from I used it a lame way:
Spoiler

As you can see in the readFile() function I use fh.readLine twice in a row. Now I would like to make a program, that reads in more data (probably 10 lines) from a file. I wouldn't like to use ten fh.readLine in a row.
I'm kinda sure that there is an easer, more elegant way of reading data, but how?

Thank you for the help!

eastar

#2 Lupus590

  • Members
  • 1,962 posts
  • LocationUK

Posted 29 June 2019 - 01:10 PM

Have you read the wiki? Specifically the return value of fs.open

Edited by Lupus590, 29 June 2019 - 01:10 PM.


#3 eastar

  • Members
  • 17 posts

Posted 29 June 2019 - 02:51 PM

View PostLupus590, on 29 June 2019 - 01:10 PM, said:

Have you read the wiki? Specifically the return value of fs.open

Sorry, I probably don't understand you, but I have no idea how that could help me.
I do know how to open a file. I also know how to read or write a line to/from a file.

What I don't know is how to read multiple lines without much hassle.

But thanks for the help!

#4 eastar

  • Members
  • 17 posts

Posted 29 June 2019 - 04:47 PM

What I specifically would like is:
If I have a file containing data like this:
on
40
off
0
on
32
off
0
on
1

How would you read that into a table? especially if the you don't know how many lines the file contains.

Thanks again! :)

Edited by eastar, 29 June 2019 - 04:47 PM.


#5 Lupus590

  • Members
  • 1,962 posts
  • LocationUK

Posted 29 June 2019 - 07:08 PM

how does the data get into the file? If it's a program that you wrote then the easist way is to use texturils.serialise and unserialise

#6 Dog

  • Members
  • 1,177 posts
  • LocationEarth orbit

Posted 29 June 2019 - 08:38 PM

For the format you're using, I'd suggest using io.lines to read the file...
--# read the data
local myTable = { } --# declare and localize your table that will hold the data
for data in io.lines("/myFile") do --# start a loop that stops at the last data line in the file
  myTable[#myTable + 1] = data --# add the next line of data to the next table entry
end

--# print the data
for i = 1, #myTable do
  print(myTable[i])
end


#7 eastar

  • Members
  • 17 posts

Posted 30 June 2019 - 01:47 PM

View PostDog, on 29 June 2019 - 08:38 PM, said:

For the format you're using, I'd suggest using io.lines to read the file...
--# read the data
local myTable = { } --# declare and localize your table that will hold the data
for data in io.lines("/myFile") do --# start a loop that stops at the last data line in the file
  myTable[#myTable + 1] = data --# add the next line of data to the next table entry
end

--# print the data
for i = 1, #myTable do
  print(myTable[i])
end

Thank you for the help! :)
Sadly in my version of CC io.lines() doesn't exist. (I'm using CC 1.5)
But thank you! I'l try something similar with h.readLine()!

#8 Dog

  • Members
  • 1,177 posts
  • LocationEarth orbit

Posted 30 June 2019 - 04:32 PM

Using h.readLine() you could do this...
local myTable = { }
local myFile = fs.open("/myFile", "r")
repeat --# start a loop that ends with a condition
  local line = myFile.readLine()
  if line then --# if the line read is not nil then add it to the table
    myTable[#myTable + 1] = line
  end
until line == nil --# end the loop if the line read is nil
myFile.close()


#9 eastar

  • Members
  • 17 posts

Posted 01 July 2019 - 07:31 PM

View PostDog, on 30 June 2019 - 04:32 PM, said:

Using h.readLine() you could do this...
local myTable = { }
local myFile = fs.open("/myFile", "r")
repeat --# start a loop that ends with a condition
  local line = myFile.readLine()
  if line then --# if the line read is not nil then add it to the table
	myTable[#myTable + 1] = line
  end
until line == nil --# end the loop if the line read is nil
myFile.close()

OMG!!!
Thank you so much! :)

In the meantime I realised Lupus950 made a typo and meant textutils.serialize() and not textureils.serialize(). So I went with that one. But I'm glad you showed me this method. It makes sense now why there are loops that test the conditions at the end of the loop!

Big thanks to both of ya! :)

Edited by eastar, 01 July 2019 - 07:31 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users