Reading Data from Files Locate the document in its SAP Library structure

To read data from a file on the application server, use the READ DATASET statement:

Syntax

READ DATASET <dsn> INTO <f> [LENGTH <len>].

This statement reads data from the file <dsn> into the variable <f>. In order to determine into which variable you should read data from a file, you need to know the structure of the file.

You can specify the transfer mode in the OPEN DATASET statement. If you have not already opened the file for reading, 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.

If the system was able to read data successfully, SY-SUBRC is set to 0. When the end of the file is reached, SY-SUBRC is set to 4. If the file could not be opened, SY-SUBRC is set to 8.

If you are working in binary mode, you can use the LENGTH addition to find out the length of the data transferred to <f>. The system sets the value of the variable <len> to this length.

Example

DATA FNAME(60) VALUE 'myfile'.

DATA: TEXT1(12) VALUE 'abcdefghijkl',
      TEXT2(5),
      LENG TYPE I.

OPEN DATASET FNAME FOR OUTPUT IN BINARY MODE.
TRANSFER TEXT1 TO FNAME.
CLOSE DATASET FNAME.

OPEN DATASET FNAME FOR INPUT IN BINARY MODE.
DO.
  READ DATASET FNAME INTO TEXT2 LENGTH LENG.
  WRITE: / SY-SUBRC, TEXT2, LENG.
  IF SY-SUBRC <> 0.
    EXIT.
  ENDIF.
ENDDO.
CLOSE DATASET FNAME.

The output is:

0 abcde 5
0 fghij 5
4 kl### 2

This example fills the file "myfile" with 12 bytes from the field TEXT1. It is then read into the field TEXT2 in 5-byte portions. Note here that the system fills up the last three bytes of TEXT2 with zeros after the end of the file has been reached. The number of bytes transferred is contained in the field LENG.

If you are working in text mode, you can use the LENGTH addition to find out the length of the current line in the file. The system sets the value of the variable <len> to the length of the line. The system calculates this by counting the number of bytes between the current position and the next end of line marker in the file.

Example

DATA FNAME(60) VALUE 'myfile'.

DATA: TEXT1(4) VALUE '1234    ',
      TEXT2(8) VALUE '12345678',
      TEXT3(2),
      LENG TYPE I.

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

OPEN DATASET FNAME FOR INPUT IN TEXT MODE.
   DO 2 TIMES.
      READ DATASET FNAME INTO TEXT3 LENGTH LENG.
      WRITE: / TEXT3, LENG.
   ENDDO.
CLOSE DATASET FNAME.

The output appears as follows:

12 4

12 8

This example writes the strings TEXT1 and TEXT2 to the file "myfile" in text mode. They are then read into the string TEXT3 (length 2). The amount of memory occupied by the lines is read into the field LENG.

 

 

 

 

Leaving content frame