Reading Binary Files

A binary file is a file whose data is not organized into lines using new-line characters. In most cases, you use the character I/O methods (such as CHARS, CHARIN, CHAROUT) on these files.

Suppose, for example, that you want to read the data in the CHORD.WAV file (supplied with Windows multimedia support in c:\winnt) into a variable:

/* GETCHORD - reads CHORD.WAV into a variable           */"c:\winnt\chord.wav")
say "Number of characters in the file=" chordf~chars

/* Read the whole WAV file into a single Rexx variable. */
/* Rexx variables are limited by available memory.      */
say "Number of characters read into variable" mychord~length

The CHARIN method returns a string of characters from the stream, which in this case is CHORD.WAV. CHARIN accepts two optional arguments. If no arguments are specified, CHARIN reads one character from the current read position and then advances the read pointer.

The first argument is a start position for reading the file. In the example, 1 is specified so that CHARIN begins reading with the first character of the file. Omitting the first argument achieves the same result.

The second argument specifies how many characters are to be read. To read all the characters, infile~chars was specified as the second argument. The CHARS method returns the number of characters remaining to be read in the input stream receiving the message. CHARIN then returns all the characters in the stream. CHORD.WAV has about 25000 characters.