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 structure with separator into file

Former Member
0 Likes
2,364

hi,

I must write a file into a filesystem. The file should include data from a structure (e.g. -> data: i_bkpf like bkpf.). I must write the fields of the structure into the file separated with an character like '*'.

How can I do this in high-performance way?

thanks

markus

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,351

Hi,

Use CONCATENATE statement.

ex.

data : lv_line(1000).

loop at itab.

clear lv_line.

concatenate itab-field1

itab-field2

.

.

itab-fieldn

into lv_line separated by '*'.

transfer lv_line to filepath.

endloop.

This will be possible only when all the fields in itab are of type c or s, otherwise it will give a syntax error.

In that case, use FM SO_STRUCT_TO_CHAR to convert the structure to charecter and then concatenate and transfer.

Hope it helps.

Regards,

Shashank

4 REPLIES 4
Read only

andreas_mann3
Active Contributor
0 Likes
1,351

hi,

use assign command:


data sep value '*'.

LOOP AT ibkpf.
clear str.
DO.
ASSIGN COMPONENT sy-index OF STRUCTURE ibkpf TO <fw>.
IF sy-subrc <> 0.
EXIT.
ENDIF.
CONCATENATE str <fw> INTO str SEPARATED BY sep.
ENDDO.

transfer str to file.

ENDLOOP.

Read only

Former Member
0 Likes
1,352

Hi,

Use CONCATENATE statement.

ex.

data : lv_line(1000).

loop at itab.

clear lv_line.

concatenate itab-field1

itab-field2

.

.

itab-fieldn

into lv_line separated by '*'.

transfer lv_line to filepath.

endloop.

This will be possible only when all the fields in itab are of type c or s, otherwise it will give a syntax error.

In that case, use FM SO_STRUCT_TO_CHAR to convert the structure to charecter and then concatenate and transfer.

Hope it helps.

Regards,

Shashank

Read only

rainer_hbenthal
Active Contributor
0 Likes
1,351

Hi,

i wrote a class for that. The two main methods might be helpful:


METHOD add_item .

  DATA:
    x       TYPE string,
    sep     TYPE c,
    c       TYPE c,
    len     TYPE i,
    start   TYPE i,
    end     TYPE i,
    status  TYPE i,
    i       TYPE i.

  x      = item.

  len    = STRLEN( x ).
  status =  0.
  start  = -1.
  end    =  0.

  DO len TIMES.
    i = sy-index - 1.
    c = x+i(1).

    CASE status.

      WHEN 0.
        IF c <> ' '.
          status = 1.
          start  = i.
        ENDIF.

      WHEN 1.
        IF c = ' '.
          status = 2.
          end    = i - 1.
        ENDIF.

      WHEN 2.
        IF c <> ' '.
          status = 1.
          end    = 0.
        ENDIF.

    ENDCASE.

  ENDDO.

  IF start = -1.
    x = ''.
  ELSEIF end = 0.
    x = x+start.
  ELSE.
    len = end - start + 1.
    x = x+start(len).
  ENDIF.

  IF STRLEN( x ) = 8.
    IF cd = 'I' OR cd = 'i' OR
       cd = 'D' OR cd = 'd'.

      IF date_separator IS INITIAL.
        sep = '.'.
      ELSE.
        sep = date_separator.
      ENDIF.

      CONCATENATE x+6(2) sep x+4(2) sep x+0(4)
        INTO x IN CHARACTER MODE.

    ENDIF.

    IF cd = 'E' OR cd = 'e' OR
       cd = 'A' OR cd = 'a'.

      IF date_separator IS INITIAL.
        sep = '/'.
      ELSE.
        sep = date_separator.
      ENDIF.

      CONCATENATE x+4(2) sep x+6(2) sep x+0(4)
        INTO x IN CHARACTER MODE.

    ENDIF.
  ENDIF.

  CONCATENATE wa_line x delimiter
    INTO wa_line IN CHARACTER MODE.

ENDMETHOD.



METHOD add_line .

  APPEND wa_line TO it_csv.
  CLEAR wa_line.

ENDMETHOD.

Additem strips leading and trailing blanks and can convert dates into humanreadable dates. Perhaps you dont need evrything in there.

Read only

Former Member
0 Likes
1,351

Hi Markus,

See this code , copy it and run it.

REPORT ZKUN_FILE3 .

DATA : BEGIN OF itab1 OCCURS 0,

sno TYPE i,

mtype(2),

count TYPE i,

END OF itab1.

  • Creating the contents of Table itab1.

itab1-sno = 1.

itab1-mtype = 'e1'.

itab1-count = 1.

APPEND itab1.

itab1-sno = 1.

itab1-mtype = 'e2'.

itab1-count = 3.

APPEND itab1.

itab1-sno = 2.

itab1-mtype = 'e2'.

itab1-count = 2.

APPEND itab1.

itab1-sno = 3.

itab1-mtype = 'e3'.

itab1-count = 2.

APPEND itab1.

WRITE 😕 'content of itab1'.

LOOP AT itab1.

WRITE : / itab1-sno, itab1-mtype,itab1-count LEFT-JUSTIFIED .

ENDLOOP.

CALL FUNCTION 'GUI_DOWNLOAD'

EXPORTING

  • BIN_FILESIZE =

filename = 'c:\file_test.txt'

FILETYPE = 'ASC'

  • APPEND = ' '

WRITE_FIELD_SEPARATOR = '*'

  • HEADER = '00'

  • TRUNC_TRAILING_BLANKS = ' '

  • WRITE_LF = 'X'

  • COL_SELECT = ' '

  • COL_SELECT_MASK = ' '

  • DAT_MODE = ' '

  • CONFIRM_OVERWRITE = ' '

  • NO_AUTH_CHECK = ' '

  • CODEPAGE = ' '

  • IGNORE_CERR = ABAP_TRUE

  • REPLACEMENT = '#'

  • WRITE_BOM = ' '

  • TRUNC_TRAILING_BLANKS_EOL = 'X'

  • IMPORTING

  • FILELENGTH =

tables

data_tab = itab1

  • FIELDNAMES =

EXCEPTIONS

FILE_WRITE_ERROR = 1

NO_BATCH = 2

GUI_REFUSE_FILETRANSFER = 3

INVALID_TYPE = 4

NO_AUTHORITY = 5

UNKNOWN_ERROR = 6

HEADER_NOT_ALLOWED = 7

SEPARATOR_NOT_ALLOWED = 8

FILESIZE_NOT_ALLOWED = 9

HEADER_TOO_LONG = 10

DP_ERROR_CREATE = 11

DP_ERROR_SEND = 12

DP_ERROR_WRITE = 13

UNKNOWN_DP_ERROR = 14

ACCESS_DENIED = 15

DP_OUT_OF_MEMORY = 16

DISK_FULL = 17

DP_TIMEOUT = 18

FILE_NOT_FOUND = 19

DATAPROVIDER_EXCEPTION = 20

CONTROL_FLUSH_ERROR = 21

OTHERS = 22

.

IF sy-subrc <> 0.

  • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

  • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

if sy-subrc = 0.

message i007(zmsg_kunal).

endif.

Here the file is written to the drive c with name file_test.

Hope this solves your doubt.

Do reard with points if satisfied.

Regards,

Kunal.