0% found this document useful (0 votes)
107 views3 pages

Reading Data From Files - ABAP Programming (BC-ABA) - SAP Library

Uploaded by

Sai Kiran
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
107 views3 pages

Reading Data From Files - ABAP Programming (BC-ABA) - SAP Library

Uploaded by

Sai Kiran
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 3

3/7/2015 Reading 

Data from Files ­ ABAP Programming (BC­ABA) ­ SAP Library

The Best­Run Businesses Run SAP

SAP Business Suite SAP ERP SAP R/3 and SAP R/3 Enterprise

Reading Data from Files 
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.

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.

http://help.sap.com/saphelp_470/helpdata/en/fc/eb3d42358411d1829f0000e829fbfe/content.htm 1/3
3/7/2015 Reading Data from Files ­ ABAP Programming (BC­ABA) ­ SAP Library

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.

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.
 
 
 
 

http://help.sap.com/saphelp_470/helpdata/en/fc/eb3d42358411d1829f0000e829fbfe/content.htm 2/3
3/7/2015 Reading Data from Files ­ ABAP Programming (BC­ABA) ­ SAP Library

©   C OPYR I GH T   BY  SAP  SE  OR   AN   SAP  AF F I LI AT E  C OM PAN Y.   ALL  R I GH T S  R ESER VED .   –

PR I N T ED   F R OM   SAP  H ELP  POR T AL.   (ht t p: / / help. s ap. c om )

http://help.sap.com/saphelp_470/helpdata/en/fc/eb3d42358411d1829f0000e829fbfe/content.htm 3/3

You might also like