Jump to content




Read Certain File Areas?


6 replies to this topic

#1 SNWLeader

  • Members
  • 71 posts
  • LocationTampa Bay, Florida

Posted 26 January 2013 - 02:42 PM

I know that they have the fs.open("string", "read or write")

but is there a way to make it read only a certain part of the file?

#2 grand_mind1

  • Members
  • 207 posts

Posted 26 January 2013 - 02:49 PM

Why would you need this? Reading the entire file seems pretty good to me.

#3 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 26 January 2013 - 03:12 PM

If you mean say extracting a particular line then you could do something like this
local function readLine( path, lineNum )
  local handle = fs.open( path, "r" )
  local line
  for i = 1, lineNum do
	line = handle.readLine()
  end
  handle.close()
  return line
end
obviously this could be modified to ignore particular lines too. or you could modify it to read a subset of lines, but they would need to go into a table.

Edited by TheOriginalBIT, 26 January 2013 - 04:08 PM.


#4 crazyguymgd

  • Members
  • 139 posts
  • LocationUSA

Posted 26 January 2013 - 04:05 PM

 TheOriginalBIT, on 26 January 2013 - 03:12 PM, said:

If you mean say extracting a particular line then you could do something like this
local function readLine( path, line )
  local handle = fs.open( path, "r" )
  local line
  for i = 1, line do
	line = handle.readLine()
  end
  handle.close()
  return line
end
obviously this could be modified to ignore particular lines too. or you could modify it to read a subset of lines, but they would need to go into a table.

Although I would probably use two unique variable names instead of line twice :P

#5 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 26 January 2013 - 04:07 PM

 crazyguymgd, on 26 January 2013 - 04:05 PM, said:

Although I would probably use two unique variable names instead of line twice :P
Yeh probs, wrote this when I just woke up and forgot that I did that :P

Scope means it should work, but for readability its better to name them differently.

#6 crazyguymgd

  • Members
  • 139 posts
  • LocationUSA

Posted 26 January 2013 - 04:09 PM

 TheOriginalBIT, on 26 January 2013 - 04:07 PM, said:

 crazyguymgd, on 26 January 2013 - 04:05 PM, said:

Although I would probably use two unique variable names instead of line twice :P
Yeh probs, wrote this when I just woke up and forgot that I did that :P

Scope means it should work, but for readability its better to name them differently.

Haha yeah I just wanted to say something since you pointed out my error in another topic :)

#7 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 26 January 2013 - 04:56 PM

 TheOriginalBIT, on 26 January 2013 - 04:07 PM, said:

 crazyguymgd, on 26 January 2013 - 04:05 PM, said:

Although I would probably use two unique variable names instead of line twice :P/>
Yeh probs, wrote this when I just woke up and forgot that I did that :P/>

Scope means it should work, but for readability its better to name them differently.

Uh, no, the loop would throw an error. Amounts to for i = 1, nil do.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users