Jump to content

Filereadline from end of file


Recommended Posts

I am trying to read a file line by line from the end.

According to docs:

[optional] The line number to read. The first line of a text file is line 1 (not zero), last line is -1.

One would assume that -2 would be the second last line and so on, yet it doesn't appear to be the case.

Any way to read a file line by line from the end?

Link to comment
Share on other sites

Hi,

#include <file.au3>
;file to open
$file = "test.txt"
;open file in read mode
$filehnd = FileOpen ($file, 0)
;loop reading lastline to first line
For $i = _FileCountLines ($file) To 1 Step - 1
    $line = FileReadLine ($filehnd, $i)
    MsgBox (0,"",$line)
Next
FileClose($file)

;-))

Stefan

@edit: Array solution:

#include <file.au3>
Global $arlines
$file = "test.txt"
_FileReadToArray ($file, $arlines)
For $i = $arlines [0] To 1 Step - 1
    MsgBox (0,"", $arlines [$i])
Next
Edited by 99ojo
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...