Jump to content

Recommended Posts

Posted

I want to extract a few bytes from the middle of a big file (e.g. 4GB). Say, the 2MB from offset 1GB. In FileRead(), there is no offset parameter. How should I skip to the middle of a file efficiently?

Posted

try using FileReadLine(), with _FileCountLines in a For Next loop.

or, if the file is only 1 line (although not likely if it is 4GB...) use StringLen() and StringLeft(), StringRight(), or StringMid()...

What goes around comes around... Payback's a bitch.

Posted

try using FileReadLine(), with _FileCountLines in a For Next loop.

or, if the file is only 1 line (although not likely if it is 4GB...) use StringLen() and StringLeft(), StringRight(), or StringMid()...

The problem is the file is a binary file (say .avi), so i guess line number doesn't apply. By the way, is it really possible to read a single line (assume it is) of 4GB in length and at the same time passing it to StringLen(), StringLeft(), etc without causing any problem?

Posted (edited)

try using FileReadLine(), with _FileCountLines in a For Next loop.

or, if the file is only 1 line (although not likely if it is 4GB...) use StringLen() and StringLeft(), StringRight(), or StringMid()...

That's loading 4GB into memory. (I doubt AutoIt allows strings that large anyways)

This probably isn't the best way, but heh, it works.

Func _FileReadBytes($sFileName, $nStart, $nEnd)
    Local $nBytesToRead = $nEnd - $nStart
    Local $ResultBuffer = DLLStructCreate("byte[" & $nBytesToRead & "]")
    Local $BytesRead = 0
    Local $OverlappedStruct = DLLStructCreate($tagOVERLAPPED)
    Local $ResultString = ""
    DLLStructSetData($OverlappedStruct, "Offset", $nStart)
    
    $hFile = _WinAPI_CreateFile($sFileName, 2, 2, 2, 4, 0)
    if ($hFile == 0) Then return ""
    
    if (_WinAPI_ReadFile($hFile, DLLStructGetPtr($ResultBuffer), $nBytesToRead, $BytesRead, DLLStructGetPtr($OverlappedStruct)) <> true) then 
        _WinAPI_CloseHandle($hFile)
        Return ""
    EndIf
    
    For $i = 1 to $nBytesToRead + 1
        $ResultString &= Chr(DLLStructGetData($ResultBuffer, 1, $i))
    Next
    
    _WinAPI_CloseHandle($hFile)
    
    Return $ResultString
EndFunc

(If you are reading binary data, I think you might need to return the $ResultBuffer variable because AutoIt might cut the string off after the first NULL character, or call StringToBinary.)

Edited by cppman

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...