Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Reading file into itab

Former Member
0 Likes
1,115

I have to read a text file into my data objects until the end of file. I need help with this syntax

do

read dataset file into strfile

while end of file ?

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,056

hi

use the following code:

do.

read dataset <datasetname> into s_string.

if sy-subrc ne 0.

exit.

endif.

.

.

.

.

enddo.

Please award points if helpful.

Prabhas

I have to read a text file into my data objects until the end of file. I need help with this syntax

do

read dataset file into strfile

while end of file ?

7 REPLIES 7
Read only

Former Member
0 Likes
1,057

hi

use the following code:

do.

read dataset <datasetname> into s_string.

if sy-subrc ne 0.

exit.

endif.

.

.

.

.

enddo.

Please award points if helpful.

Prabhas

Read only

Former Member
0 Likes
1,056

Use WHILE instead of DO in your case.

Regards,

Amey

Read only

Former Member
0 Likes
1,056

Hi Megan,

Small corection to the code.

read dataset <filename> for input in text mode encoding default.
do.
read dataset <filename> into itab-data.
if sy-subrc = 0.
append itab.
clear itab.
else.
exit.
endif.
enddo.

close dataset <filename>.

Regards,

Ravi

Read only

Former Member
0 Likes
1,056

You have first open the file in text mode

OPEN DATASET myfile IN TEXT MODE ENCODING DEFAULT.

After that you introduce the loop to read the file record by record and if read fails you exit.

DO.
  READ DATASET myfile INTO myrecordstructure.
  IF SY-SUBRC <> 0.
*-- End of file reached
    EXIT.
  ENDIF.
......
..... rest of the processing with the current record
ENDDO.

Read only

Former Member
0 Likes
1,056

Hi Megan,

Hope this code helps you.

Start-of-selection.

open dataset fname for input in text mode.

if sy-subrc <> 0.

message s999(z1) with 'File not found or cannot be opened'.

stop.

endif.

do.

read dataset fname into i_upload.

if sy-subrc <> 0.

exit.

endif.

append i_upload.

clear i_upload.

enddo.

close dataset fname.

Thanks,

Srinivas

Read only

Former Member
0 Likes
1,056

Hi,

Please try this.


*   Retrieve Data file from Application server(Upload from Unix)

DATA: i_file like rlgrap-filename value '/usr/sap/tmp/file.txt'.

    OPEN DATASET i_file FOR INPUT IN TEXT MODE.
    IF sy-subrc NE 0.
      MESSAGE e999(za) WITH 'Error opening file' i_file.
    ENDIF.
    DO.
*     Reads each line of file individually
      READ DATASET i_file INTO wa_datatab.
*     Perform processing here
*     .....   
    ENDDO.

Regards,

Ferry Lianto

Read only

Former Member
0 Likes
1,056

Hi,

check the folowing link

http://www.sapbrain.com

regards,

Bhaskar