updating oE get_bytes


include io.e 
namespace io 
public function get_bytes(integer fn, integer n) 

reads the next bytes from a file.

  1. fn : an integer, the handle to an open file to read from.
  2. n : a positive integer, the number of bytes to read.

A sequence, of length at most n, made of the bytes that could be read from the file.


When n > 0 and the function returns a sequence of length less than n you know you have reached the end of file. Eventually, an empty sequence will be returned.

This function is normally used with files opened in binary mode, "rb". This avoids the confusing situation in text mode where Windows will convert CR LF pairs to LF.

Example 1:
integer fn 
fn = open("temp", "rb")  -- an existing file 
sequence whole_file 
whole_file = {} 
sequence chunk 
while 1 do 
    chunk = get_bytes(fn, 100) -- read 100 bytes at a time 
    whole_file &= chunk        -- chunk might be empty, that's ok 
    if length(chunk) < 100 then 
    end if 
end while 
? length(whole_file)  -- should match DIR size of "temp" 
See Also:

getc, gets, get_integer32, get_dstring

Not Categorized, Please Help


Quick Links

User menu

Not signed in.

Misc Menu