Reading Binary Files

A binary file is a file whose data is not organized into lines ending with line-end 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 in C:\\Windows\\Media) into a variable:


getchord&per;cmd
/* getchord.wav - reads chord.wav into a variable */
chordf=.stream~new("C:\\Windows\\Media\\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. */
mychord=chordf~charin(1,chordf~chars)
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, chordf~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 114,000 characters.