‎2005 Jul 13 11:23 AM
Hi all,
I have to write a log from an internal table ITAB to a file in this format:
COL1 | COL2 | COL3 | COL4
123 11 ABC 3
What should I do?
‎2005 Jul 13 11:30 AM
Hi,
you can try something like this -
DATA HEADER(72).
CONCATENATE 'COL1'
'COL2'
'COL3'
'COL4'
INTO HEADER
SEPARATED BY SPACE.
LOOP AT ITAB.
AT FIRST.
TRANSFER HEADER TO FILENAME.
ENDAT.
CONCATENATE ITAB-COL1
ITAB-COL2
ITAB-COL3
ITAB-COL4
INTO FILE_LINE
SEPARATED BY '|'.
TRANSFER FILE_LINE TO FILENAME.
ENDLOOP.Regards,
Anand Mandalika.
‎2005 Jul 13 11:28 AM
Hi,
Check this link.You can find sample code.
http://www.sts.tu-harburg.de/teaching/sap_r3/ABAP4/open_dat.htm
‎2005 Jul 13 11:30 AM
Hi,
you can try something like this -
DATA HEADER(72).
CONCATENATE 'COL1'
'COL2'
'COL3'
'COL4'
INTO HEADER
SEPARATED BY SPACE.
LOOP AT ITAB.
AT FIRST.
TRANSFER HEADER TO FILENAME.
ENDAT.
CONCATENATE ITAB-COL1
ITAB-COL2
ITAB-COL3
ITAB-COL4
INTO FILE_LINE
SEPARATED BY '|'.
TRANSFER FILE_LINE TO FILENAME.
ENDLOOP.Regards,
Anand Mandalika.
‎2005 Jul 14 2:44 AM
Hi Anand Mandalika,
The sample codes are very useful to me. Thanks a lot.
But it can't insert a break when I write the actual content after the header line, i.e. the header and the content line is on the same line in the file.
How to add a break for this?
Thanks.
‎2005 Jul 14 3:35 AM
Hi Macy,
This is something that I have not noticed before. But I've found the solution as well. Here's what you need to do -
DATA HEADER(72).
CONCATENATE 'COL1'
'COL2'
'COL3'
'COL4'
INTO HEADER
SEPARATED BY SPACE.
LOOP AT ITAB.
AT FIRST.
<b> open dataset filename for output in text mode.</b>
TRANSFER HEADER TO FILENAME.
ENDAT.
CONCATENATE ITAB-COL1
ITAB-COL2
ITAB-COL3
ITAB-COL4
INTO FILE_LINE
SEPARATED BY '|'.
TRANSFER FILE_LINE TO FILENAME.
ENDLOOP.The only difference to the earlier code, as you can see , is the OPEN DATASET statement.
Regards,
Anand Mandalika.
‎2005 Jul 14 3:48 AM
Hi Macy,
There's one more thing that you need to know here. If tany of the fields in a record of the internal table is
initial, then you will get some alignment problems for the separator (in this case, '|'). For example, if your
internal table has 3 columns and the following entries,
01 ONE SUNDAY
02 TWO MONDAY
03 TUESDAY
04 FOUR WEDNESDAY
05 FIVE THURSDAY
06 SIX FRIDAY
07 SATURDAYthen your output in the file is going to look like this -
01|ONE|SUNDAY
02|TWO|MONDAY
03||TUESDAY
04|FOUR|WEDNESDAY
05|FIVE|THURSDAY
06|SIX|FRIDAY
07||SATURDAYClearly, this is undesirable. As you can see above, this problem occurs even when the length of the content of the
fields in the internal table is uneven.
So I suggest that you do something like this -
DATA: BEGIN OF ITAB OCCURS 0,
CARRID LIKE SFLIGHT-CARRID,
SEPARATOR1,
CONNID LIKE SFLIGHT-CONNID,
SEPARATOR2,
FLDATE LIKE SFLIGHT-FLDATE,
END OF ITAB.
DATA : HEADER(20) VALUE 'SPFLI DETAILS',
FILENAME(60) VALUE <give the name of the AppServer file here>,
FILE_LINE(40).
SELECT CARRID
CONNID
FLDATE
FROM SFLIGHT
INTO CORRESPONDING FIELDS OF TABLE ITAB.
ITAB-SEPARATOR1 = '|'.
ITAB-SEPARATOR2 = '|'.
MODIFY ITAB TRANSPORTING SEPARATOR1
SEPARATOR2
WHERE SEPARATOR1 EQ SPACE.
LOOP AT ITAB.
AT FIRST.
OPEN DATASET FILENAME FOR OUTPUT IN TEXT MODE.
TRANSFER HEADER TO FILENAME.
ENDAT.
TRANSFER ITAB TO FILENAME.
ENDLOOP.Hope that make the point clear. If yes, I wouldn't mind more points :-). Otherwise, please do get back. And I'll
try to help you understand.
Regards,
Anand Mandalika.
‎2005 Jul 13 11:33 AM
If you want to write to the PC then use GUI_DOWNLOAD
To the application server use OPEN, TRANSER and CLOSE
DATA: file(20) TYPE C value '/usr/test.dat'.
OPEN DATASET file IN TEXT MODE ENCODING DEFAULT FOR OUTPUT.
IF SY-SUBRC = 0.
LOOP AT ITAB.
TRANSFER ITAB TO file.
ENDLOOP.
ENDDO.
CLOSE DATASET file.
‎2005 Jul 13 11:34 AM
Download internal table to Application server file(Unix)
DATA: e_file like rlgrap-filename value '/usr/sap/tmp/file.txt'.
open dataset e_file for output in text mode.
lOOP AT it_datatab......
transfer it_datatab to e_file.
ENDLOOP.
close dataset e_file.
Hope this helps.
Thanks & Regards,
Judith.
‎2005 Jul 13 11:40 AM
Hi Macy,
or try to do it dyn. with assign component:
(for big structures like bseg)
1) heading:
-get fieldcatalog of your itab
loop at fcat.
CONCATENATE 'ITAB-' fcat-name INTO hfield.
ASSIGN (hfield) TO <fs>.
CONCATENATE str <fs> INTO str SEPARATED BY space.
endloop.
transfer str to file.2) data
LOOP AT itab.
clear str.
DO.
ASSIGN COMPONENT sy-index OF STRUCTURE itab TO <fw>.
IF sy-subrc <> 0.
EXIT.
ENDIF.
CONCATENATE str <fw> INTO str SEPARATED BY space.
ENDDO.
transfer str to file.
ENDLOOP.regards Andreas
Message was edited by: Andreas Mann
‎2005 Jul 13 11:42 AM
Hi ,
Open the file using the open dataset statement in output mode.
Then transfer the contents of the internal table to the file.
sample code:
open dataset w_dsn for output in text mode encoding default.
LOOP AT ITAB.
IF SY-TABIX = 1.
ITAB5_OUT-MATNR = 'INPUT MATERIAL #..'.
ITAB5_OUT-WERKS = 'POM.'.
ITAB5_OUT-PHWERKS = 'PHPL'.
TRANSFER ITAB5_OUT TO DSN.
ENDIF.
MOVE-CORRESPONDING ITAB TO ITAB5_OUT.
TRANSFER ITAB5_OUT TO DSN.
ENDLOOP.
CLOSE DATASET DSN.
here itab5_out is the internal table that has the headers.Itab is the internal table that contans the actual data.
Hope this helps you.
Regards,
Venkatalakshmi