orbs

how to read the last character of a text file?

6 posts in this topic

#1 ·  Posted (edited)

i know i can read the entire file into a string and check the last character.

but there are plenty of files, and very large ones, so looking for a more efficient way, if such exists.

thanks for any hints.

EDIT: specifically, i need to know if the file ends with @LF. so i guess reading line-by-line is no good, as it splits by line feeds so i won't be able to tell if the last character was @LF and was stripped, or never existed at all.

Edited by orbs

Share this post


Link to post
Share on other sites



no good. it strips the last @LF (and the last @CRLF also). it returns the last line whether it ends with @LF/@CRLF or not.

Share this post


Link to post
Share on other sites

Not positive what your goingfor but I noticed with one CR it gives you the last printed text, with 2 it gives the the empty line feed.

with that info you could code to delete any empty lines and stop when FileReadLine returns printed text.

Then you should have one empty line feed at the end. (which is what you want,  or ?)

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

i don't want to add @LF at the end, just to know if it exists or not. and i don't want to actually read the contents of the file if i don't really have to.

 

ok, i think i found something... using FileSetPos() to jump to the last character and read it:

Func FileReadLastChar($sFile)
    Local $hFile = FileOpen($sFile)
    FileSetPos($hFile, -1, 2) ; 2 = $FILE_END, -1 = one position before file end => last char position
    Local $sLastChar = FileRead($hFile)
    FileClose($hFile)
    Return $sLastChar
EndFunc   ;==>FileReadLastChar

now to see if it's more efficient then reading the entire file... fingers crossed! 

 

EDIT: ok, either i'm doing something wrong, or it's 45x times faster! :thumbsup: (on a sample batch).

but, it's only 20x times faster on all files in "C:\Windows\system32".

i guess there's a lot of caching involved. now wondering how to eliminate that... :think:

Edited by orbs

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

You could try opening the file in binary mode to see if that makes a difference with binary files likely to be found in System32.  You might look at some binary files with a hex editor to see if a trailing LF is stored with a leading zero or as a single byte.

 

Edit: I seem to remember in hex editors seeing line feeds as one byte.  If I remember right Dos was little endian so the small nibble would be first as in 0x0A00.  But you should look for yourself to be sure as this was Dos 16 bit stuff when I was poking through binary files back in the day.

 

Edited by MilesAhead
more info

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