Jump to content

Read only the first megabyte of a VERY large file


Recommended Posts

I have a 11 GB file that I need to analyze.

So my first step is reading the first 1 Mb of the file and look at it.
I'm unable to make a working script. When running this script it crashes with message "Error allocating memory":

$file_src="in.txt" ; source file
$file_dest="out.txt" ; 

$size=1048576 ; 
$count=FileGetSize($file_src)-$size ; 

$handle_src=FileOpen($file_src,16) ; open in binary read mode
$handle_dest=FileOpen($file_dest,16+2) ; open new file in binary WRITE mode - erase previous contents if they exist

FileWrite($handle_dest,BinaryMid(FileRead($handle_src),1,$size)) ; 

; close files after using them


Link to comment
Share on other sites

Hello. Use the number of bytes to be read in FileRead's second parameter, so that way you will not need binarymid.



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

  • Recently Browsing   0 members

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