Is it possible to read the file "backwards" using Lua?

Instead of reading a file from the front, can it be read in the opposite direction? Thus, the output is on the back of the file in front of the file.

EDIT: The last line is displayed first, and not completely backward.

+4
source share
3 answers

This decision is based on the idea of ​​@ Pavel Kulchenko.
Yes, this is cumbersome :-)

Define a function io.linesbackward(filename)in the library io:

function io.linesbackward(filename)
  local file = assert(io.open(filename))
  local chunk_size = 4*1024
  local iterator = function() return "" end
  local tail = ""
  local chunk_index = math.ceil(file:seek"end" / chunk_size)
  return 
    function()
      while true do
        local lineEOL, line = iterator()
        if lineEOL ~= "" then 
          return line:reverse() 
        end
        repeat
          chunk_index = chunk_index - 1
          if chunk_index < 0 then 
            file:close()
            iterator = function() 
                         error('No more lines in file "'..filename..'"', 3) 
                       end  
            return 
          end
          file:seek("set", chunk_index * chunk_size)
          local chunk = file:read(chunk_size)
          local pattern = "^(.-"..(chunk_index > 0 and "\n" or "")..")(.*)"
          local new_tail, lines = chunk:match(pattern)
          iterator = lines and (lines..tail):reverse():gmatch"(\n?\r?([^\n]*))"
          tail = new_tail or chunk..tail
        until iterator
      end
    end
end

Using:

local filename = "your_file.txt"
print("--- backward: ---------------------------")
for line in io.linesbackward(filename) do
  print(line)
end
print("--- forward: ----------------------------")
for line in io.lines(filename) do
  print(line)
end
print("-----------------------------------------")
+2
source

It is possible, but cumbersome. The Lua API provides seek to set and retrieve the position in the file to which read / write operations are applied.

, "", (, filesize-1024, 1024 , , ) , . , , , (, , , ), , , .

+4

Do not use the standard library. But you can always read it from beginning to end, line by line, save it in a table, and then “use” it from the last line to the first.

+1
source

All Articles