Using Binary Mode Locate the document in its SAP Library structure

To open a file in binary mode, use the IN BINARY MODE addition to the OPEN DATASET statement.

Syntax

OPEN DATASET <dsn> IN BINARY MODE [FOR ....].

If you read from or write to a file that is open in binary mode, the data is transferred byte by byte. The system does not interpret the contents of the file while it is being transferred. If you write the contents of a field to a file, the system transfers all of the bytes in the source field. When you transfer data from a file to a field, the number of bytes transferred depends on the length of the target field. If you then use another ABAP statement to address the target field, the system interprets the field contents according to the data type of the field.

Example

DATA FNAME(60) VALUE 'myfile'.

DATA: NUM1     TYPE I,
      NUM2     TYPE I,
      TEXT1(4) TYPE C,
      TEXT2(8) TYPE C,
      HEX      TYPE X.

OPEN DATASET FNAME FOR OUTPUT IN BINARY MODE.

NUM1  = 111.
TEXT1 = 'TEXT'.
TRANSFER NUM1  TO FNAME.
TRANSFER TEXT1 TO FNAME.

OPEN DATASET FNAME FOR INPUT IN BINARY MODE.

READ DATASET FNAME INTO TEXT2.
WRITE / TEXT2.

OPEN DATASET FNAME FOR INPUT IN BINARY MODE.

READ DATASET FNAME INTO NUM2.
WRITE / NUM2.

OPEN DATASET FNAME FOR INPUT IN BINARY MODE.

SKIP.
DO.
  READ DATASET FNAME INTO HEX.
  If SY-SUBRC <> 0.
    EXIT.
  ENDIF.
  WRITE  HEX.
ENDDO.

The output appears as follows:

###oTEXT

111

00 00 00 6F 54 45 58 54

The program opens the file "myfile" in binary mode and writes the contents of the fields NUM1 and TEXT1 into the file. For information about the TRANSFER statement, refer to Writing Data to Files. The file is then opened for reading, and its entire contents are read into the field TEXT2. For information about the READ DATASET statement, refer to Reading Data from Files. The first four characters of the string TEXT2 are nonsense, since the corresponding bytes are the platform-specific representation of the number 111. The system tries to interpret all of the bytes as characters. However, this only works for the last four bytes. After the OPEN statement, the position is reset to the start of the file, and the first four bytes of the file are transferred into NUM2. The value of NUM2 is correct, since it has the same data type as NUM1. Finally, the eight bytes of the file are read into the field HEX. On the screen, you can see the hexadecimal representation of the file contents. The last four bytes are the ASCII representation of the characters in the word "TEXT".

 

 

 

 

Leaving content frame