写入文件程序
*&---------------------------------------------------------------------*
*& Report ZTCXHTEST_WRITE_FILE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZTCXHTEST_WRITE_FILE.
DATA: fname(60) TYPE c VALUE 'samplefile.txt'.
TYPES: BEGIN OF line,
col1(1) TYPE c,
col2(10) TYPE c,
END OF line.
TYPES itab TYPE line OCCURS 10.
DATA: lin TYPE line,
tab TYPE itab.
DO 5 TIMES.
lin-col1 = sy-index.
lin-col2 = sy-index ** 2.
APPEND lin TO tab.
ENDDO.
OPEN DATASET fname FOR OUTPUT IN TEXT MODE ENCODING UTF-8.
LOOP AT tab INTO lin.
TRANSFER lin TO fname.
ENDLOOP.
CLOSE DATASET fname.
----------------------------------------------
读取文件
*&---------------------------------------------------------------------*
*& Report ZTCXHTEST_READ_FILE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZTCXHTEST_READ_FILE.
DATA: fname(60) TYPE c VALUE 'samplefile2.txt',
text1(12) TYPE c VALUE 'abcdefghijkl',
text2(5) TYPE c,
len TYPE i.
OPEN DATASET fname FOR OUTPUT IN BINARY MODE.
TRANSFER text1 TO fname.
CLOSE DATASET fname.
OPEN DATASET fname FOR INPUT IN BINARY MODE.
DO.
READ DATASET fname INTO text2 LENGTH len.
WRITE: / sy-subrc, text2, len.
IF sy-subrc <> 0.
EXIT.
ENDIF.
ENDDO.
CLOSE DATASET fname.
------------------------------------------------
*&---------------------------------------------------------------------*
*& Report ZTCXHTEST_OPERATE_ON_APPSERVER 将结构体写入文件
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZTCXHTEST_OPERATE_ON_APPSERVER.
TABLES bkpf.
DATA dsn(20) TYPE c VALUE 'bbkpf.dat'.
DATA rec LIKE bbkpf.
OPEN DATASET dsn FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
IF sy-subrc <> 0.
EXIT.
ENDIF.
SELECT * FROM bkpf UP TO 10 ROWS.
CLEAR rec.
rec-stype = '1'.
rec-tcode = 'FB01'.
rec-bldat = sy-datum.
rec-bukrs = bkpf-bukrs.
rec-belnr = bkpf-belnr.
rec-blart = bkpf-blart.
TRANSFER rec TO dsn.
ENDSELECT.
CLOSE DATASET dsn.
OPEN DATASET dsn FOR INPUT IN TEXT MODE ENCODING DEFAULT.
DO.
READ DATASET dsn INTO rec.
IF sy-subrc <> 0.
EXIT.
ENDIF.
WRITE: / rec.
ENDDO.
CLOSE DATASET dsn.