# Reading in files, keeping \*ALL\* info from file

**URL:** <https://www.ruby-forum.com/t/reading-in-files-keeping-all-info-from-file/65076>\
**Category:** Ruby\
**Created:** [August 15, 2006, 11:31pm UTC](https://www.ruby-forum.com/t/reading-in-files-keeping-all-info-from-file/65076 "2006-08-15T23:31:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [August 15, 2006, 11:31pm UTC](https://www.ruby-forum.com/t/reading-in-files-keeping-all-info-from-file/65076/1 "2006-08-15T23:31:16Z")

</div>

Hi,

I am trying to read in a file and parse it. It uses hex values, such  
as 0x0000 to identify certain tags. When I read this into  
Ruby(IO.read), it simply gives me a blank string: “” However, I need to  
be able to get 0x0000 from my input. The same is true of 0x0001, which  
returns the same as 0x1, but I need the leading 0’s.

I have attempted to use the lower lever sysread an syswrite, but to no  
avail. I have the same problem and in addition, when asked to read  
0x0000 it returns an (IOError) where read simply returns “”.

Is there any way for me to read this information in and keep every byte?

Thank you

Chris

---

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [August 16, 2006, 12:06am UTC](https://www.ruby-forum.com/t/reading-in-files-keeping-all-info-from-file/65076/2 "2006-08-16T00:06:59Z")

</div>

Works fine here, maybe you should send how you do read your file

[~/tmp]% cat test\_read.rb  
data = File.open(ARGV[0]) do |file|  
file.read  
end  
puts data.inspect

[~/tmp]% echo -n ‘\000\000\000\001’ \> file  
[~/tmp]% ruby test\_read.rb file  
“\000\000\000\001”

Regards,  
Sylvain J.

---

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [August 16, 2006, 12:19am UTC](https://www.ruby-forum.com/t/reading-in-files-keeping-all-info-from-file/65076/3 "2006-08-16T00:19:02Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Sylvain J. wrote:

> data = File.open(ARGV[0]) do |file|  
> file.read  
> end  
> puts data.inspect

This code can be shortened as thus:

p ARGF.read

-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.2.2 (GNU/Linux)

iD8DBQFE4kcnmV9O7RYnKMcRAs5dAJ475eHEz7BmrL+qVQP2f3OE2Y6IkQCgmae8  
LO7exDURrnYncZQ17UOQjHs=  
=Qn+B  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [August 16, 2006, 10:52pm UTC](https://www.ruby-forum.com/t/reading-in-files-keeping-all-info-from-file/65076/4 "2006-08-16T22:52:01Z")

</div>

Chris Binc wrote:

> 0x0000 it returns an (IOError) where read simply returns “”.
> 
> Is there any way for me to read this information in and keep every byte?

Since you’re reading binary data, here’s what I’d do

bytes = File.open(f, “rb”) {|io| io.read}

Kind regards

```
robert
```
