# Reading/writing binary problem

**URL:** <https://www.ruby-forum.com/t/reading-writing-binary-problem/89988>\
**Category:** Ruby\
**Created:** [March 29, 2006, 4:26pm UTC](https://www.ruby-forum.com/t/reading-writing-binary-problem/89988 "2006-03-29T16:26:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![wolfgang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/54ee81/32.png) [@wolfgang](https://www.ruby-forum.com/u/wolfgang)\
**Post date:** [March 29, 2006, 4:26pm UTC](https://www.ruby-forum.com/t/reading-writing-binary-problem/89988/1 "2006-03-29T16:26:53Z")

</div>

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.

Thanks  
Wolfgang

---

<div class="post-metadata">

**Author:** ![wolfgang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/54ee81/32.png) [@wolfgang](https://www.ruby-forum.com/u/wolfgang)\
**Post date:** [March 29, 2006, 5:15pm UTC](https://www.ruby-forum.com/t/reading-writing-binary-problem/89988/2 "2006-03-29T17:15:48Z")

</div>

Wolfgang wrote:

> ```
> [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
```

---

<div class="post-metadata">

**Author:** ![wolfgang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/54ee81/32.png) [@wolfgang](https://www.ruby-forum.com/u/wolfgang)\
**Post date:** [March 29, 2006, 5:43pm UTC](https://www.ruby-forum.com/t/reading-writing-binary-problem/89988/3 "2006-03-29T17:43:27Z")

</div>

On Wed, 29 Mar 2006, Wolfgang wrote:

> Wolfgang  
> this sould be easy if i understood that method and the nature of your  
> data 😉

here’s what i understand this method to do :

# open the file

```
 file=fopen(filename);

```

# read all available unit16 quantities into an [m, n] array

```
 f= fread(file,'uint16');

```

# check the size of this array, r is the number of elements read

```
 [r,k]=size(f);

```

# k will be one iff data was read into r dimension

```
 if k==1;

```

# here i assume 1048576 is the ‘row\_size’ of 1mb. we set t to be the

offset

# of the last row

```
 t=r-1048576;

```

# because matlab is evil and uses one based indexing, we inc t by one

and

# extract the last row/block of data. but here it seems like the ith

index

# should be t+1:r-1048576 no? otherwise it seems like this would be

out of

# bounds?

```
 m=f(t+1:r,:);

```

in any case this looks alot like you are trying to extract the last 1mb  
of  
uint16 quantities from a data file - is that right? let me know and  
i’ll show  
you how to do this.

regards.

-a

---

<div class="post-metadata">

**Author:** ![wolfgang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/54ee81/32.png) [@wolfgang](https://www.ruby-forum.com/u/wolfgang)\
**Post date:** [March 29, 2006, 6:32pm UTC](https://www.ruby-forum.com/t/reading-writing-binary-problem/89988/4 "2006-03-29T18:32:15Z")

</div>

On Thu, 30 Mar 2006, Wolfgang wrote:

> PS: I will also test the example of Robert

ok. so, just to clarify, you want to read a 2mb matrix out of a file  
after  
skiping 13312 bytes of crap. this will do that

def extract\_data path  
open(path){|f| f.seek(13312); f.read(1024_1024_2)}  
end

here data returned is a buffer. if you want a matrix object i  
**highly**  
suggest installing the narray module and using something like

require ‘narray’  
def extract\_matrix path  
na = NArray.sint 1024, 1024  
open(path){|f| f.seek(13312); na[] = f.read(1024_1024_2)}  
na  
end

this will return a numerical matrix with nice operators. you can write  
it to  
file with

open(‘m.dat’,‘w’){|f| f.write na}

and this will write the matix in binary.

regards.

-a

---

<div class="post-metadata">

**Author:** ![wolfgang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/54ee81/32.png) [@wolfgang](https://www.ruby-forum.com/u/wolfgang)\
**Post date:** [March 29, 2006, 6:41pm UTC](https://www.ruby-forum.com/t/reading-writing-binary-problem/89988/5 "2006-03-29T18:41:19Z")

</div>

On Mar 29, 2006, at 6:31 PM, removed\_email\_address@domain.invalid wrote:

> open(path){|f| f.seek(13312); f.read(1024_1024_2)}

If you are using Windows don’t forget to open it in binary mode:

open(…, ‘rb’) …

– Daniel

---

<div class="post-metadata">

**Author:** ![wolfgang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/54ee81/32.png) [@wolfgang](https://www.ruby-forum.com/u/wolfgang)\
**Post date:** [March 29, 2006, 6:16pm UTC](https://www.ruby-forum.com/t/reading-writing-binary-problem/89988/6 "2006-03-29T18:16:58Z")

</div>

Hello,

yes I want to extract a matrix of 1024x1024 of binary data (2bytes for  
each value). But ahead of this data is 1048576 bit (13312 bytes) of  
crap. I don’t know matlab at all and I don’t like to start this routine  
when I want to import that data into my application, which can not skip  
this header. If it is easier to do by a linux command (its running in  
cygwin) any help is also welcome.

Wolfgang

PS: I will also test the example of Robert

removed\_email\_address@domain.invalid schrieb:

---

<div class="post-metadata">

**Author:** ![wolfgang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/54ee81/32.png) [@wolfgang](https://www.ruby-forum.com/u/wolfgang)\
**Post date:** [March 29, 2006, 6:59pm UTC](https://www.ruby-forum.com/t/reading-writing-binary-problem/89988/7 "2006-03-29T18:59:28Z")

</div>

On Thu, 30 Mar 2006, Daniel H. wrote:

> On Mar 29, 2006, at 6:31 PM, removed\_email\_address@domain.invalid wrote:
> 
> > open(path){|f| f.seek(13312); f.read(1024_1024_2)}
> 
> If you are using Windows don’t forget to open it in binary mode:
> 
> open(…, ‘rb’) …
> 
> – Daniel

ah. indeed.

-a

---

<div class="post-metadata">

**Author:** ![wolfgang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/54ee81/32.png) [@wolfgang](https://www.ruby-forum.com/u/wolfgang)\
**Post date:** [March 29, 2006, 8:15pm UTC](https://www.ruby-forum.com/t/reading-writing-binary-problem/89988/8 "2006-03-29T20:15:37Z")

</div>

Thank you all,

this worked perfectly!

Wolfgang

removed\_email\_address@domain.invalid schrieb:

---

<div class="post-metadata">

**Author:** ![wolfgang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/54ee81/32.png) [@wolfgang](https://www.ruby-forum.com/u/wolfgang)\
**Post date:** [March 29, 2006, 6:56pm UTC](https://www.ruby-forum.com/t/reading-writing-binary-problem/89988/9 "2006-03-29T18:56:26Z")

</div>

Daniel H. wrote:

> On Mar 29, 2006, at 6:31 PM, removed\_email\_address@domain.invalid wrote:
> 
> > open(path){|f| f.seek(13312); f.read(1024_1024_2)}
> 
> If you are using Windows don’t forget to open it in binary mode:
> 
> open(…, ‘rb’) …

In fact I recommend to use that switch regardless of OS used: it’s more  
portable and also serves as documentation hint that the file is actually  
binary.

Kind regards

```
robert
```
