SAP NetWeaver AS ABAP Release 751, ©Copyright 2017 SAP AG. All rights reserved.
READ DATASET dset INTO dobj [MAXIMUM LENGTH mlen]
[[ACTUAL] LENGTH alen].
This statement exports data from the file specified in dset to the data object dobj . dobj expects variables with elementary data types and flat structures. The file must be opened using an access type. If a closed or nonexistent file is accessed, a handleable exception is raised. If the file was opened as a text file, dobj must be character-like.
dset expects a character-like data object containing the physical name of the file. The content is read from the file starting from the current file pointer. After the data is passed, the file pointer is positioned after the section that was read. Using the MAXIMUM LENGTH addition, the number of characters or bytes to be read from the file can be limited. Using ACTUAL LENGTH , the number of characters or bytes actually used can be determined.
Influence of the Access Type
Files can be read independently of the access type. Whether data can be read or not depends solely on the position of the file pointer. If the latter is at the end of the file or after the file, no data can be read and sy-subrc will be set to 4.
Influence of the Storage Type
The import function works regardless of the storage type used to open the file with the statement OPEN DATASET .
If the specified storage type makes conversion necessary, this is executed before the assignment to the data object dobj . Afterwards, the read data is placed, byte by byte, into the data object.
Read Text Files
sy-subrc | Meaning |
0 | Data is read up to an explicit end-of-line marker or up to an implicit end-of-line marker at the end of the file. |
4 | An attempt was made to read data after the end of the file. |
Read Binary Files
sy-subrc | Meaning |
0 | Data was read; either the system did not reach the end of the of file or the system stopped reading at exactly the end of the file. |
4 | Data was read up to the end of the file and the target field was longer than necessary, or an attempt was made to read data after the end of the file. |
Imports the binary file flights.dat written in the example by the statement TRANSFER . The data is written (in binary) to a byte-like typed field symbol . The assignment of the structured data area wa to the field symbol applies the length of the data area and imports a corresponding number of bytes for the loop process. It would be possible to import directly into the structure wa with the same result, but the use of the field symbol is the recommended procedure. The reason is that in this way data is passed explicitly from a binary file into a binary data type.
DATA: file TYPE string VALUE `flights.dat`,
wa TYPE spfli,
itab LIKE TABLE OF wa.
OPEN DATASET file FOR INPUT IN BINARY MODE.
ASSIGN wa TO CASTING.
DO.
READ DATASET file INTO .
IF sy-subrc = 0.
APPEND wa TO itab.
ELSE.
EXIT.
ENDIF.
ENDDO.
CLOSE DATASET file.
. MAXIMUM LENGTH mlen
This addition determines how many characters or how many bytes at most are read from the file. mlen expects a data object of the type i . It contains the number of characters or bytes. In the case of text files, the content of mlen determines how many characters are read from the file. In the case of binary files, legacy text files, and legacy binary files, mlen determines how many bytes are read from the file.
The first mlen characters or bytes are read from the current position of the file pointer and the file pointer is positioned after the read file. If the file is opened as a (legacy) text file and there is an end-of-line marker within the specified length, data is read only up to this position and the file pointer is positioned after the end-of-line marker.
If the value of mlen is the same as 0, no data is read. If the value of mlen is negative, the addition is ignored and the import takes place in the same way as described for Influence of the Storage Type .
In case of text files, the number of bytes read depends on the character format specified using ENCODING when opening the file.
This program section has the same functions as the previous example. Here data is imported not into a byte-like field symbol, but into a byte-like data object hex_container . The number of bytes to be imported is determined by the typed field symbol .
DATA: file TYPE string VALUE `flights.dat`,
hex_container TYPE x LENGTH 1000,
len TYPE i,
itab TYPE TABLE OF spfli.
FIELD-SYMBOLS TYPE spfli.
DESCRIBE FIELD LENGTH len IN BYTE MODE.
OPEN DATASET file FOR INPUT IN BINARY MODE.
ASSIGN hex_container TO CASTING.
DO.
READ DATASET file INTO hex_container MAXIMUM LENGTH len.
IF sy-subrc = 0.
APPEND TO itab.
ELSE.
EXIT.
ENDIF.
ENDDO.
CLOSE DATASET file.
. [ACTUAL] LENGTH alen
This addition assigns the number of characters or bytes to be read from the file to the data object alen . The following can be specified for alen :
For text files, the system determines how many characters were written to the memory area. For binary files, legacy text files, and legacy binary files, the system determines how many bytes were read from the file.