Writing Data to Files 

To write data to a file on the application server, use the TRANSFER statement:

Syntax

TRANSFER <f> to <dsn> [LENGTH <len>].

This statement writes the values of the field <f> into the file <dsn>. You can specify the transfer mode in the OPEN DATASET statement. If you have not already opened the file for writing, the system tries to open it either in binary mode, or using the additions from the last OPEN DATASET statement. However, it is good practice only to open files using the OPEN DATASET statement. For further information about the OPEN DATASET statement and the naming conventions for files, refer to Opening a File. <f> can have an elementary data type, but may also be a structure, as long as it does not contain an internal table. You cannot write internal tables into files in a single step.

You can specify the length of the data you want to transfer using the LENGTH addition. The system then transfers the first <len> bytes into the file. If <len> is too short, excess bytes are truncated. If <len> is greater than the length of the field, the system adds trailing blanks.

The following program shows how you can write internal tables into a file:

DATA FNAME(60) VALUE 'myfile'.

TYPES: BEGIN OF LINE,
         COL1 TYPE I,
         COL2 TYPE I,
       END OF LINE.

TYPES  ITAB TYPE LINE OCCURS 10.

DATA: LIN TYPE LINE,
      TAB TYPE ITAB.

DO 5 TIMES.
  LIN-COL1 = SY-INDEX.
  LIN-COL2 = SY-INDEX ** 2.
  APPEND LIN TO TAB.
ENDDO.

OPEN DATASET FNAME FOR OUTPUT.
LOOP AT TAB INTO LIN.
  TRANSFER LIN TO FNAME.
ENDLOOP.
CLOSE DATASET FNAME.

OPEN DATASET FNAME FOR INPUT.
DO.
  READ DATASET FNAME INTO LIN.
  IF SY-SUBRC <> 0.
    EXIT.
  ENDIF.
  WRITE: / LIN-COL1, LIN-COL2.
ENDDO.

CLOSE DATASET FNAME.

The output is:

1 1
2 4
3 9
4 16
5 25

In this example, a structure LIN and an internal table TAB with line type LINE are created. Once the internal table TAB has been filled, it is written line by line into the file "myfile". The file is then read into the structure LIN, whose contents are displayed on the screen.

The following example works in R/3 Systems that are running on UNIX systems using ASCII.

DATA FNAME(60) VALUE 'myfile'.

DATA: TEXT1(4) VALUE '1234',
      TEXT2(8) VALUE '12345678',
      HEX  TYPE X.

OPEN DATASET FNAME FOR OUTPUT IN TEXT MODE.
TRANSFER: TEXT1 TO FNAME LENGTH 6,
          TEXT2 TO FNAME LENGTH 6.
CLOSE DATASET FNAME.

OPEN DATASET FNAME FOR INPUT.
DO.
  READ DATASET FNAME INTO HEX.
  IF SY-SUBRC <> 0.
    EXIT.
  ENDIF.
  WRITE  HEX.
ENDDO.
CLOSE DATASET FNAME.

The output is:

31 32 33 34 20 20 0A 31 32 33 34 35 36 0A

This example writes the strings TEXT1 and TEXT2 to the file "myfile" in text mode. The output length is set to 6. By reading the file byte by byte into the hexadecimal field, you can see how it is stored. The numbers 31 to 36 are the ASCII codes for the digits 1 to 6. 20 is a space, and 0A marks the end of the line. TEXT1 is filled with two trailing spaces. Two characters are truncated from TEXT2.