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

Read file into internal table using OPEN DATASET FOR INPUT

Former Member
0 Likes
24,596

Hi

i want to save a internal table (simple structure) to a file on the application server using the OPEN DATASET x FOR OUTPUT IN TEXT MODE (...). I don't have a problem creating the file - so far so good.

Afterwards i want to load the same file into a internal table.

The OPEN DATASET x FOR INPUT IN TEXT MODE (...) and the following READ DATASET x INTO y doean't accept the original structure as the input structure y.

Is there an easy way to "press" a input file line into a structure if the structure that was used to create the file is known?

Example:

1. Create file using the internal table it_data (structure ls_data):

OPEN DATASET file FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.

LOOP AT it_data INTO ls_data.

TRANSFER ls_data TO file.

ENDLOOP.

2. Read the file into the table (NOT WORKING)

OPEN DATASET file FOR INPUT IN TEXT MODE ENCODING DEFAULT.

DO.

READ DATASET file INTO ls_data. " NOT WORKING !!!

IF sy-subrc EQ 0.

APPEND ls_data TO it_data.

ELSE.

EXIT.

ENDIF.

ENDDO:

The second step is not working because only a string structure seems to be accepted to read a dataset into.

Does anybody know a easy way to read the file into the internal table?

Thank you

Manfred

1 ACCEPTED SOLUTION
Read only

kesavadas_thekkillath
Active Contributor
7,392

Read it to a string then split it.


data:it_string type table of string.
field-symbols:<fs> type string,
                      <fs_f> type any.

OPEN DATASET file FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
LOOP AT it_data INTO ls_data.
do.
assign component sy-index of structure ls_data to <fs_f>.
if sy-subrc = 0.
if sy-index = 1.
wf_string = <fs_f> .
else.
concatenate wf_string <fs_f> separated by '|'.
endif.
else.
transfer wf_string to file.
exit.
endif.
enddo.
ENDLOOP.


OPEN DATASET file FOR INPUT IN TEXT MODE ENCODING DEFAULT.
DO.
READ DATASET file INTO wf_string. 
 IF sy-subrc EQ 0.
  clear it_string[].
  split wf_string at '|' into table it_string. "assuming '|' as separator
  loop at it_string assigning <fs>.
   assign component sy-tabix of structure ls_data to <fs_f>.
   if sy-subrc = 0.
    <fs_f> = <fs>.
   endif.
    at last.
    append ls_data to it_data.
   endat.
  endloop.
ELSE.
EXIT.
ENDIF.
ENDDO:

Hi

i want to save a internal table (simple structure) to a file on the application server using the OPEN DATASET x FOR OUTPUT IN TEXT MODE (...). I don't have a problem creating the file - so far so good.

Afterwards i want to load the same file into a internal table.

The OPEN DATASET x FOR INPUT IN TEXT MODE (...) and the following READ DATASET x INTO y doean't accept the original structure as the input structure y.

Is there an easy way to "press" a input file line into a structure if the structure that was used to create the file is known?

Example:

1. Create file using the internal table it_data (structure ls_data):

OPEN DATASET file FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.

LOOP AT it_data INTO ls_data.

TRANSFER ls_data TO file.

ENDLOOP.

2. Read the file into the table (NOT WORKING)

OPEN DATASET file FOR INPUT IN TEXT MODE ENCODING DEFAULT.

DO.

READ DATASET file INTO ls_data. " NOT WORKING !!!

IF sy-subrc EQ 0.

APPEND ls_data TO it_data.

ELSE.

EXIT.

ENDIF.

ENDDO:

The second step is not working because only a string structure seems to be accepted to read a dataset into.

Does anybody know a easy way to read the file into the internal table?

Thank you

Manfred

3 REPLIES 3
Read only

kesavadas_thekkillath
Active Contributor
7,393

Read it to a string then split it.


data:it_string type table of string.
field-symbols:<fs> type string,
                      <fs_f> type any.

OPEN DATASET file FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
LOOP AT it_data INTO ls_data.
do.
assign component sy-index of structure ls_data to <fs_f>.
if sy-subrc = 0.
if sy-index = 1.
wf_string = <fs_f> .
else.
concatenate wf_string <fs_f> separated by '|'.
endif.
else.
transfer wf_string to file.
exit.
endif.
enddo.
ENDLOOP.


OPEN DATASET file FOR INPUT IN TEXT MODE ENCODING DEFAULT.
DO.
READ DATASET file INTO wf_string. 
 IF sy-subrc EQ 0.
  clear it_string[].
  split wf_string at '|' into table it_string. "assuming '|' as separator
  loop at it_string assigning <fs>.
   assign component sy-tabix of structure ls_data to <fs_f>.
   if sy-subrc = 0.
    <fs_f> = <fs>.
   endif.
    at last.
    append ls_data to it_data.
   endat.
  endloop.
ELSE.
EXIT.
ENDIF.
ENDDO:

Read only

0 Likes
7,391

Thank you

i already though about the ASSIGN COMPONENT command. But as you described it it seems quite easy to handle.

Is it possible to use a TAB as a delimiter?

Regards

Manfred

Read only

0 Likes
7,391

Is it possible to use a TAB as a delimiter?

Yes .


concatenate wf_string <fs_f> separated by CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB.
split wf_string at  CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB into table i_string .