In similar question people recommend use File.read to read a whole file. But when I try to read png file (see fig. 1) I get only first line (see fig. 2). What am I doing wrong?
Asked
Active
Viewed 228 times
0
-
check this link [how to read image file in ruby][1] [1]: http://stackoverflow.com/questions/3334179/how-to-read-image-file-in-ruby – davidesantangelo May 06 '15 at 15:37
1 Answers
3
Use File.binread
to read binary data.
On certain operating systems (notably Windows), there is a difference between opening a file in "binary mode" (8-bit characters) and "text mode" (7-bit characters). Because of this, these IO implementations can do things like detect end-of-file when there is a zero character, or mangle up characters outside of the ASCII range if you don't tell them to expect binary data.
If you open a file in Ruby, using mode "rb" instead of "r" will tell the OS that you expect binary data, and if it cares about that, it will do the right thing. File.binread()
opens the underlying file it will read from with that mode.

BadZen
- 4,083
- 2
- 25
- 48