Writing Binary Files

To write a binary file, you use CHAROUT. CHAROUT writes only the characters that you specify in an argument of the method. CHAROUT does not add line-end characters to the end of the string. Here is an example:


jack&per;cmd
/* jack&per;cmd - demonstrate that CHAROUT does not add line-end characters */
filebin=.stream~new("binary.dat") /* Create a new stream object */
filebin~open("replace") /* Open the file for replacement */
do i=1 to 50 /* Write fifty strings */
filebin~charout("All work and no play makes Jack a dull boy. ")
end
filebin~close /* Close the file so we can display it */
"type binary.dat" /* Use the TYPE command to display file */

Because line-end characters are not added, the text displayed by the TYPE command is concatenated into one contiguous line.

CHAROUT writes the string specified and advances the write pointer. If you want to position the write pointer before writing the string, specify the starting position as a second argument:

filebin~charout("Jack is losing it.",30) /* start writing at character 30 */

In the example, the file is explicitly opened and closed. If you do not open the file, Rexx attempts to open the file for both reading and writing. If you do not close the file, Rexx closes it when the procedure ends.