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

Write file contents dynamically

Former Member
0 Likes
453

Hi all,

I'm reading a file from the application server using OPEN DATASET as follows:

DO.

READ DATASET ds_file INTO l_string.

IF sy-subrc NE 0.

EXIT.

ENDIF.

SPLIT l_string AT con_tab INTO TABLE lt_itab.

LOOP AT lt_itab INTO wa_itab.

?????

ENDLOOP.

ENDDO.

lt_itab consists of one field 25 characters long. I want to assign each row in lt_itab to a specific field in a work area dynamically...maybe using a field symbol...can I do that? I don't want to have to name each field in the work area....

regards,

Mat

1 ACCEPTED SOLUTION
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
384

Here is a little more complete example, instead this uses comma as the delimiter.



report zrich_0001.

types: begin of ttab,
       fld1(10) type c,
       fld2(10) type c,
       fld3(10) type c,
       end   of ttab.
data: itab type table of ttab.
data: wa like line of itab.


data: istr type table of string.
data: xstr type string.

data:
      dsn(100) value '/usr/sap/TST/sys/test.txt'.

field-symbols: <fs>.

clear itab.  refresh itab.

* Read the data.
open dataset dsn for input in text mode.
do.
  read dataset dsn into xstr.
  if sy-subrc = 0.
    split xstr at ',' into table istr.
    loop at istr into xstr.
      assign component sy-tabix of structure wa to <fs>.
      if sy-subrc <> 0.
        exit.
      endif.
      <fs> = xstr.
    endloop.
    append wa to itab.
  else.
    exit.
  endif.
enddo.
close dataset dsn.


loop at itab into wa.
  write:/ wa-fld1, wa-fld2, wa-fld3.
endloop.

Here is how the file looks.



ABC,DEF,GHI
JKL,MNO,PQR
STV,WVX,YZ0

Regards,

Rich Heilman

2 REPLIES 2
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
384

If the values which are in the table IT_ITAB are in the same order as the fields in the target structure then you can do this.

DO.
READ DATASET ds_file INTO l_string.

IF sy-subrc NE 0.
EXIT.
ENDIF.

SPLIT l_string AT con_tab INTO TABLE lt_itab.
field-symbols: <fs> .
LOOP AT lt_itab INTO wa_itab.
 
 assign component sy-tabix of structure wa_struc to <fs>.
   if sy-subrc <> 0.
     exit.
  endif.
<fs> = wa_itab.

ENDLOOP.
ENDDO.

Regards,

Rich Heilman

Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
385

Here is a little more complete example, instead this uses comma as the delimiter.



report zrich_0001.

types: begin of ttab,
       fld1(10) type c,
       fld2(10) type c,
       fld3(10) type c,
       end   of ttab.
data: itab type table of ttab.
data: wa like line of itab.


data: istr type table of string.
data: xstr type string.

data:
      dsn(100) value '/usr/sap/TST/sys/test.txt'.

field-symbols: <fs>.

clear itab.  refresh itab.

* Read the data.
open dataset dsn for input in text mode.
do.
  read dataset dsn into xstr.
  if sy-subrc = 0.
    split xstr at ',' into table istr.
    loop at istr into xstr.
      assign component sy-tabix of structure wa to <fs>.
      if sy-subrc <> 0.
        exit.
      endif.
      <fs> = xstr.
    endloop.
    append wa to itab.
  else.
    exit.
  endif.
enddo.
close dataset dsn.


loop at itab into wa.
  write:/ wa-fld1, wa-fld2, wa-fld3.
endloop.

Here is how the file looks.



ABC,DEF,GHI
JKL,MNO,PQR
STV,WVX,YZ0

Regards,

Rich Heilman