Champak Posted February 7, 2009 Share Posted February 7, 2009 (edited) 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 February 7, 2009 by Champak Link to comment Share on other sites More sharing options...
KaFu Posted February 7, 2009 Share Posted February 7, 2009 (edited) #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 February 7, 2009 by KaFu OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2022-Nov-26) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Feb-16) HMW - Hide my Windows (2018-Sep-16) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2023-Jun-03) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16) Link to comment Share on other sites More sharing options...
Champak Posted February 7, 2009 Author Share Posted February 7, 2009 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. Link to comment Share on other sites More sharing options...
KaFu Posted February 7, 2009 Share Posted February 7, 2009 (edited) 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 February 7, 2009 by KaFu OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2022-Nov-26) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Feb-16) HMW - Hide my Windows (2018-Sep-16) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2023-Jun-03) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16) Link to comment Share on other sites More sharing options...
Champak Posted February 7, 2009 Author Share Posted February 7, 2009 Cool, thanks. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now