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

Files in ABAP

Hi all,

How to work with file interfaces. OPEN DATASET etc. I want to upload file data into internal table from Application server?

Thanks in advance.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

6 Answers

  • Posted on Jan 15, 2007 at 05:26 AM

    hi,

    parameters: p_file like rlgrap-filename obligatory
    default '/usr/sap/upload.xls'.
    
    types: begin of t_data,
    vbeln like vbap-vbeln,
    posnr like vbap-posnr,
    matnr like vbap-matnr,
    werks like vbap-werks,
    megne like vbap-zmeng,
    end of t_data.
    data: it_data type standard table of t_data,
    wa_data type t_data.
    
    open dataset p_file for output in text mode encoding default.
    if sy-subrc ne 0.
    write:/ 'Unable to open file:', p_file.
    else.
    do.
    read dataset p_file into wa_data.
    if sy-subrc ne 0.
    exit.
    else.
    append wa_data to it_data.
    endif.
    enddo.
    close dataset p_file.
    endif.

    Regards

    Anver

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 15, 2007 at 05:26 AM

    refer my demo code to download/upload file from/to Application server -

    &----


    *& Report ZGILL_AS *

    *& *

    &----


    *& *

    *& *

    &----


    REPORT ZGILL_AS message-id rp .

    tables: pa0001,pa0002.

    select-options s_pernr for pa0001-pernr no intervals MODIF ID XYZ.

    parameters: p_dwnld AS CHECKBOX ,

    p_upld AS CHECKBOX DEFAULT 'X'.

    parameters: P_DSNI(75) TYPE C MODIF ID ABG DEFAULT

    '/usr/local/sapdata/amit.dat' LOWER CASE.

    data: begin of itab occurs 0,

    pernr(8),

    sp1(1) value ',',

    werks(4),

    sp2(1) value ',',

    persg(1),

    sp3(1) value ',',

    persk(2),

    end of itab.

    data: s_eof(3).

    start-of-selection.

    if p_upld = 'X'.

    OPEN DATASET P_DSNI FOR OUTPUT IN LEGACY TEXT MODE.

    PERFORM FETCH_DATA.

    STOP.

    elseif p_dwnld = 'X'.

    OPEN DATASET P_DSNI FOR INPUT IN LEGACY TEXT MODE.

    IF SY-SUBRC NE 0.

    MESSAGE E016 WITH

    'Error opening seq. file, RC:' SY-SUBRC.

    EXIT.

    ENDIF.

    CLEAR S_EOF.

    DO.

    PERFORM FETCH_file.

    IF S_EOF EQ 'YES'. stop. ENDIF.

    ENDDO.

    endif.

    END-OF-SELECTION.

    if itab[] is not initial.

    perform print_file1 tables itab.

    else.

    write:/ 'No records exists'.

    endif.

    &----


    *& Form FETCH_DATA

    &----


    • text

    ----


    • --> p1 text

    • <-- p2 text

    ----


    FORM FETCH_DATA .

    SELECT * FROM PA0001 WHERE PERNR IN S_PERNR.

    MOVE-CORRESPONDING PA0001 TO ITAB.

    TRANSFER ITAB TO P_DSNI.

    APPEND ITAB.

    ENDSELECT.

    CLOSE DATASET P_DSNI.

    ENDFORM. " FETCH_DATA

    &----


    *& Form FETCH_file

    &----


    • text

    ----


    • --> p1 text

    • <-- p2 text

    ----


    FORM FETCH_file .

    READ DATASET P_DSNI INTO itab.

    append itab.

    clear itab.

    IF SY-SUBRC NE 0.

    S_EOF = 'YES'. EXIT.

    ENDIF.

    ENDFORM. " FETCH_file

    &----


    *& Form print_file1

    &----


    • text

    ----


    • -->P_ITAB text

    ----


    FORM print_file1 tables P_ITAB structure itab .

    write:/2 'EmpNo',

    14 'Personnel Area',

    34 'Emp Group',

    47 'Emp SubGroup'.

    skip 1.

    loop at p_itab.

    write:2 p_itab-pernr,

    14 p_itab-werks,

    34 p_itab-persg,

    47 p_itab-persk.

    skip 1.

    endloop.

    ENDFORM. " print_file1

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 15, 2007 at 05:26 AM

    Hi

    Use this code :

    Retrieve Data file from Application server

    DATA: i_file like rlgrap-filename value '/usr/sap/tmp/file.txt'.

    OPEN DATASET i_file FOR INPUT IN TEXT MODE.

    IF sy-subrc NE 0.

    MESSAGE e999(za) WITH 'Error opening file' i_file.

    ENDIF.

    DO.

    • Reads each line of file individually

    READ DATASET i_file INTO wa_datatab.

    • Perform processing here

    • .....

    ENDDO.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 15, 2007 at 05:27 AM
    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jan 15, 2007 at 05:29 AM

    • OPEN DATASET ‘TEST.TXT’ FOR OUTPUT/INPUT /APPENDING IN TEXTMODE/BINARYMODE ENCODING DEFAULT

    • O/P- moving data from itab to seq file

    • I/p –moving data from seq file to itab

    • Appending –this is also move data from itab to seq file but it doesn’t overwrites the existing content file , it adds content at last

    • Open dataset- creating seqfile in app layer and open it to accept data

    • Textmode- moving data line by line

    • Binarymode – movin data char by char generally used in grtaphical

    • Encoding default – converting data fornat to default format of seqfile i.e to Unicode or non-unicode format

    • Transfer- to move data from itab to seqfile

    • Never use sy-subrc after transfer because it doesn’t refer sy-subrc

    • Close data set ‘test.txt’

    • Delete datset ‘test.txt’

    Program

    Open dataset ‘test.txt’ for input in textmode encoding default.

    If sy-subrc <> 0

    WriteL:’error’

    Else do

    Read dataset ‘test.txt’ into itab2

    If sy-subrc<> 0

    Append itab2

    Clear itab2

    Else exit

    Endif

    Enddo

    Close dataset ‘test.txt’

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jan 15, 2007 at 05:29 AM

    Hi,

    Chcek this sample code and kindly reward points if it helps.

    DATA: BEGIN OF T_INPUT OCCURS 0, " Flat file data

    f1(9) TYPE C,

    f2(15) TYPE C,

    END OF T_INPUT.

    data G_AFILE1(100) TYPE C,

    G_DATA(1000) TYPE C,

    G_DELIM TYPE C VALUE CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB.

    OPEN DATASET G_AFILE1 FOR INPUT IN TEXT MODE ENCODING DEFAULT.

    IF SY-SUBRC = 0.

    DO.

    READ DATASET G_AFILE1 INTO G_DATA.

    IF SY-SUBRC = 0.

    SPLIT G_DATA AT G_DELIM INTO T_INPUT-f1 T_INPUT-f2.

    IF SY-SUBRC = 0.

    APPEND T_INPUT.

    CLEAR T_INPUT.

    ENDIF.

    ELSE.

    EXIT. "After reading last Record

    ENDIF.

    ENDDO.

    ELSE.

    MESSAGE E001 WITH "Error in opening File'.

    ENDIF.

    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.