Skip to Content
author's profile photo Former Member
Former Member

Urgent

which is a statement used to write a record to a file in ABAP?

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

1 Answer

  • author's profile photo Former Member
    Former Member
    Posted on Sep 10, 2007 at 07:47 AM

    Hi,

    To write data to a file on the application server, use the TRANSFER statement:

    <b>Syntax</b>

    TRANSFER <f> to <dsn> [LENGTH <len>].

    This statement writes the values of the field <f> into the file <dsn>. You can specify the transfer

    mode in the OPEN DATASET statement. If you have not already opened the file for writing, the

    system tries to open it either in binary mode, or using the additions from the last OPEN

    DATASET statement. However, it is good practice only to open files using the OPEN DATASET

    statement. For further information about the OPEN DATASET statement and the naming

    conventions for files, refer to Opening a File. <f> can have an elementary data type,

    but may also be a structure, as long as it does not contain an internal table. You cannot write

    internal tables into files in a single step.

    You can specify the length of the data you want to transfer using the LENGTH addition. The

    system then transfers the first <len> bytes into the file. If <len> is too short, excess bytes are

    truncated. If <len> is greater than the length of the field, the system adds trailing blanks.

    The following program shows how you can write internal tables into a file:

    DATA FNAME(60) VALUE 'myfile'.

    TYPES: BEGIN OF LINE,

    COL1 TYPE I,

    COL2 TYPE I,

    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.

    LOOP AT TAB INTO LIN.

    TRANSFER LIN TO FNAME.

    ENDLOOP.

    CLOSE DATASET FNAME.

    OPEN DATASET FNAME FOR INPUT.

    DO.

    READ DATASET FNAME INTO LIN.

    IF SY-SUBRC <> 0.

    EXIT.

    ENDIF.

    WRITE: / LIN-COL1, LIN-COL2.

    ENDDO.

    CLOSE DATASET FNAME.

    The output is:

    1 1

    2 4

    3 9

    4 16

    5 25

    In this example, a structure LIN and an internal table TAB with line type LINE are

    created. Once the internal table TAB has been filled, it is written line by line into the

    file “myfile”. The file is then read into the structure LIN, whose contents are

    displayed on the screen.

    Regards,

    Bhaskar

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.