Expand/Shrink

get_bytes

Definition: include get.e
sequence s = get_bytes(integer fn, integer i)
Description: Read the next i bytes from file number fn. Return the bytes as a sequence. The sequence will be of length i, except when there are fewer than i bytes remaining to be read in the file.
pwa/p2js: Not supported.
Comments: When i > 0 and length(s) < i you know you have reached the end of file. Eventually, an empty sequence will be returned for s.

This function is normally used with files opened in binary mode, "rb". This avoids the confusing situation in text mode where DOS will convert CR LF pairs to LF.
Example:
include get.e
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
        exit
    end if
end while
close(fn)
? length(whole_file)  -- should match DIR size of "temp"
See Also: getc, gets