Wolfgang wrote:
> Hi,
> 
> I have some binary files which I need to delete some data at the 
> beginning of the file (to extract the raw data out of the file). I got a 
> matlab file from a colleque but I would prefer if I could do it with ruby.
> 
> This matlab code does the trick:
>     function m=write_rad(filename)
>     file=fopen(filename);
>     f= fread(file,'uint16');
> 
>     [r,k]=size(f);
>     if k==1;
>            t=r-1048576;
>            m=f(t+1:r,:);
>     end
>     end % function
> 
> How can I do this in ruby? I'm completely lost.

An easy solution is to copy the file:

Untested:

def cut_prefix(file, length)
   raise ArgumentError, "negative length" if length < 0

   bak = file + ".bak"
   File.rename file, bak

   File.open(bak,"rb") do |io_r|
     io_r.seek length

     File.open(file, "wb") do |io_w|
       while ( buffer = io_r.read(4096) )
         io_w.write(buffer)
       end
     end
   end
end

Another solution would be to do it internally with continuously seeking, 
reading, seeking, writing etc.

Kind regards

	robert