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 carriage-return and line-feed characters to the end of the string. Here is an example:

/* JACK.CMD - demonstrate that CHAROUT does not add new-line 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 new-line characters are not added, the text displayed by the TYPE command is concatenated.

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 loosing 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. If you omit the CLOSE method in the example, the TYPE command does not type the file. To Windows, the file does not exist yet because it is not closed yet.