Reading a binary file / writing binary data to a file 
Author Message
 Reading a binary file / writing binary data to a file

I'm having troubles while reading binary file with under
Windows. Probably, I'm forgetting something while opening
a file but I can't figure out what.

Currently I'm using: ruby 1.6.2 (2000-11-04) [i386-mingw32]
under Windows NT.

The following code opens a file with binary data. Although
the size of binaryfile.dat is a few hundred bytes it only
reads one byte. There is no difference in using the methods
getc, gets or sysread.

  File.open ("binaryfile.dat", "r") { |f|
    while ch = f.getc
      puts "read a character: #{ch}"
    end
  }

Also writing binary data to file causes problems. Note,
everything works fine while reading plain text.

Please help me.

Regards,
Paul.



Tue, 26 Aug 2003 19:38:00 GMT  
 Reading a binary file / writing binary data to a file

 Try in binary mode

P>   File.open ("binaryfile.dat", "r") { |f|

     File.open ("binaryfile.dat", "rb") { |f|

P>     while ch = f.getc
P>       puts "read a character: #{ch}"
P>     end
P>   }

Guy Decoux



Tue, 26 Aug 2003 19:43:57 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Reading and writing binary data from files

2. Reading/writing arbitrary binary data files

3. Reading binary files, unflatten binary string

4. binary file read/write acces

5. reading/writing binary files.

6. Writing binary data to a file without carriage returns every 512 bytes

7. How do I write binary data in LabVIEW to a Matlab MAT-file

8. Binary Files Read and Write

9. Writing binary data to a file

10. Reading and writing to binary files.

11. reading/writing binary files in Python

12. Writing binary files in C to be read in MS FORTRAN 4.0

 

 
Powered by phpBB® Forum Software