support23,
Попробуйте так.
Код:
#include <Array.au3>
Dim $aError[5] = ['Успех', 'Не существует файла:', 'Пустой файл:', 'Ошибка открытия файла:', 'Ошибка чтения файла:']
$sFile = @ScriptFullPath
;$sFile = @ScriptDir & '\1.txt'
$iCount = Random(5, 15, 1)
ConsoleWrite($iCount & @LF)
$aLast = _FileReadLastLine($sFile, $iCount)
If @error Then
MsgBox(16, 'Error', $aError[@error] & @LF & $sFile)
Else
_ArrayDisplay($aLast, $aError[@error])
EndIf
Func _FileReadLastLine($s_File, $i_LastCount)
;$s_File - полный путь к файлу
;$i_LastCount - количество возвращаемых с конца строк
;Вернет массив $a_Ret с $i_LastCount последними строками, $a_Ret[0] = $i_LastCount
;пустые строки удаляются.
Local $h_File, $i_Size, $s_Text, $aTemp, $i_Pos, $f_ExitLoop, $i_Start = 2, $a_Text, $a_Ret[2]
If Not FileExists($s_File) Then Return SetError(1, 0, '')
$i_Size = FileGetSize($s_File)
If Not $i_Size Then Return SetError(2, 0, '')
$h_File = FileOpen($s_File, 0)
If $h_File = -1 Then Return SetError(3, 0, '')
$i_Pos = $i_LastCount + 1
While 1
$s_Text = ''
FileSetPos($h_File, -$i_Pos, 2)
$s_Text = FileRead($h_File)
$s_Text = StringRegExpReplace(StringStripCR($s_Text), '([\n]+$)', '')
$s_Text = StringRegExpReplace($s_Text, '(\n){2,}', '$1')
If $f_ExitLoop Then ExitLoop
StringReplace($s_Text, @LF, '')
If @extended >= $i_LastCount Then ExitLoop
$i_Pos += $i_LastCount + 1
If $i_Pos > $i_Size Then
$f_ExitLoop = True
$i_Start = 1
$i_Pos = $i_Size
EndIf
WEnd
FileClose($h_File)
If Not $s_Text Then Return SetError(4, 0, '')
$a_Text = StringSplit($s_Text, @LF)
$s_Text = ''
If $a_Text[0] > 1 Then
If $f_ExitLoop Then
ReDim $a_Ret[$a_Text[0] + 1]
Else
ReDim $a_Ret[$a_Text[0]]
EndIf
For $i = $i_Start To $a_Text[0]
$a_Ret[0] += 1
$a_Ret[$a_Ret[0]] = StringStripWS($a_Text[$i], 7)
Next
Else
$a_Ret[0] = 1
$a_Ret[1] = StringStripWS($a_Text[1], 7)
EndIf
Return $a_Ret
EndFunc ;==>_FileReadLastLine
|