Jump to content
Sign in to follow this  
SublimePorte

Filereadline from end of file

Recommended Posts

SublimePorte

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?

Share this post


Link to post
Share on other sites
99ojo

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

Share this post


Link to post
Share on other sites
SublimePorte

Ahh should've checked the udf's for a function to countlines. Thanks.

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.