Jump to content
Sign in to follow this  
Champak

Simple question, but can't figure it

Recommended Posts

Champak

How do I detect a line break at the end of a text file?

I tried If FileReadLine("file", -1) = "" but that didn't do it. A couple other things, but none worked,

Edited by Champak

Share this post


Link to post
Share on other sites
KaFu

#include <winapi.au3>
local $nBytes, $tBuffer_save
$filename = @ScriptDir & '\test1.txt'
$FileSize = FileGetSize($filename)

$hFile = _WinAPI_CreateFile($filename, 2, 2)

$tBuffer = DllStructCreate("byte[" & 1 & "]"); 1 Byte
_WinAPI_SetFilePointer($hFile, $FileSize - 1) ; 1 Byte
_WinAPI_ReadFile($hFile, DllStructGetPtr($tBuffer), 1, $nBytes) ; 1 Byte

$tBuffer_save = asc(BinaryToString(DllStructGetData($tBuffer, 1)))

Switch $tBuffer_save
    case 10
        MsgBox(0,'','Last character of file ' & @crlf & @crlf & $filename & @crlf & @crlf & ' IS a line feed' & @crlf & @crlf & 'ASCII-Code: ' & $tBuffer_save & @crlf & @crlf & 'Char:' & chr($tBuffer_save))
    case Else
        MsgBox(0,'','Last character of file ' & @crlf & @crlf & $filename & @crlf & @crlf & ' is not a line feed.' & @crlf & @crlf & 'ASCII-Code: ' & $tBuffer_save & @crlf & @crlf & 'Char:' & chr($tBuffer_save))
EndSwitch

Detects a line-feed at the end (chr(10)). A windows line break consists of the two characters @crlf, or chr(13)+chr(10). If you want to detect the linebreak, read the two last byte, split the return value and check if first is chr(13) and last is chr(10).

Edited by KaFu

Share this post


Link to post
Share on other sites
Champak

I decided to use _FileReadToArray() and check the last element if it is empty. However, I would like to try this one and see if it is faster or not, but you didn't supply one of the functions _WinAPI_SetFilePointer(). If you could pass that along, thanks.

Share this post


Link to post
Share on other sites
KaFu

Update to AU 3.3.0.0 :) ... it's now in the UDF winapi.au3:

Func _WinAPI_SetFilePointer($hFile, $iPos, $iMethod = 0)
    Local $aResult

    $aResult = DllCall("kernel32.dll", "long", "SetFilePointer", "hwnd", $hFile, "long", $iPos, "long_ptr", 0, "long", $iMethod)
    If @error Then Return SetError(1, 0, -1)
    If $aResult[0] = $__WINAPCONSTANT_INVALID_SET_FILE_POINTER Then Return SetError(2, 0, -1)
    Return $aResult[0]
EndFunc ;==>_WinAPI_SetFilePointer

And for sure this one is faster as it directly jumps to the last byte instead of reading the whole file.

I decided to use _FileReadToArray() and check the last element if it is empty. However, I would like to try this one and see if it is faster or not, but you didn't supply one of the functions _WinAPI_SetFilePointer(). If you could pass that along, thanks.

FileReadToArray() loads the whole file into memory. Hard with really large files. And if the last element is empty, it's not necessarily a linebreak. Edited by KaFu

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.