Jump to content

how to read the last character of a text file?


orbs
 Share

Recommended Posts

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

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

FileReadLine has a parameter that allows you to read the last line:

worth a shot...never used it though.

[optional] The line number to read. The first line of a text file is line 1 (not zero), last line is -1.
Link to comment
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.

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
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 ?)

Link to comment
Share on other sites

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

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

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
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...