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

dataset sample code

Former Member
0 Likes
680

Hi All!

I am having a .DAT file on the application server.Now i want to download into my internal tables using Datasets.

The format of the data is as below..

000123450987

The first 4 places represent one field

second 3 places represent another field

the last 5 places represent another field.

There is no separator in between.Can any body provide me the sample code for this requirement.

Regards

Pavan

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
577

DATA: l_car_ret(1) TYPE c, " To store carriage return character

l_lin_fed(1) TYPE c, " To store Line feed character.

OPEN DATASET p_file FOR INPUT

IN TEXT MODE

ENCODING DEFAULT.

CLEAR l_car_ret.

CLEAR l_lin_fed.

MOVE cl_abap_char_utilities=>cr_lf+0(1) TO l_car_ret.

MOVE cl_abap_char_utilities=>cr_lf+1(1) TO l_lin_fed.

DO.

TRY.

  • Read the current row of the file

READ DATASET p_file INTO w_filedata.

IF sy-subrc NE 0.

EXIT. "End of file entries

ENDIF.

CATCH cx_root.

  • 'Invalid input file format :Extraneous column'(048).

ENDTRY.

  • Remove the trailing carriage return character, if any

SEARCH w_filedata FOR l_car_ret.

IF sy-subrc EQ 0.

CLEAR l_pos.

MOVE sy-fdpos TO l_pos.

REPLACE w_filedata+l_pos(1) IN w_filedata WITH space.

ENDIF.

  • Remove the trailing line feed character, if any

SEARCH w_filedata FOR l_lin_fed.

IF sy-subrc EQ 0.

CLEAR l_pos.

MOVE sy-fdpos TO l_pos.

REPLACE w_filedata+l_pos(1) IN w_filedata WITH space.

ENDIF.

w_data-field1 = w_filedata+0(4).

w_data-field2 = w_filedata+4(3).

w_data-field3 = w_filedata+7(5).

APPEND w_data TO t_data.

CLEAR w_data.

ENDDO.

CLOSE DATASET p_file.

3 REPLIES 3
Read only

Former Member
0 Likes
578

DATA: l_car_ret(1) TYPE c, " To store carriage return character

l_lin_fed(1) TYPE c, " To store Line feed character.

OPEN DATASET p_file FOR INPUT

IN TEXT MODE

ENCODING DEFAULT.

CLEAR l_car_ret.

CLEAR l_lin_fed.

MOVE cl_abap_char_utilities=>cr_lf+0(1) TO l_car_ret.

MOVE cl_abap_char_utilities=>cr_lf+1(1) TO l_lin_fed.

DO.

TRY.

  • Read the current row of the file

READ DATASET p_file INTO w_filedata.

IF sy-subrc NE 0.

EXIT. "End of file entries

ENDIF.

CATCH cx_root.

  • 'Invalid input file format :Extraneous column'(048).

ENDTRY.

  • Remove the trailing carriage return character, if any

SEARCH w_filedata FOR l_car_ret.

IF sy-subrc EQ 0.

CLEAR l_pos.

MOVE sy-fdpos TO l_pos.

REPLACE w_filedata+l_pos(1) IN w_filedata WITH space.

ENDIF.

  • Remove the trailing line feed character, if any

SEARCH w_filedata FOR l_lin_fed.

IF sy-subrc EQ 0.

CLEAR l_pos.

MOVE sy-fdpos TO l_pos.

REPLACE w_filedata+l_pos(1) IN w_filedata WITH space.

ENDIF.

w_data-field1 = w_filedata+0(4).

w_data-field2 = w_filedata+4(3).

w_data-field3 = w_filedata+7(5).

APPEND w_data TO t_data.

CLEAR w_data.

ENDDO.

CLOSE DATASET p_file.

Read only

Former Member
0 Likes
577

Hi

Try following:

data : itab(255) occurs 0 with header line.

data : begin of itab1 occurs 0,

f1(4),

f2(3),

f3(5),

end of itab1.

open data set <name> for input in text mode encoding default.

do.

read dataset <name> into itab.

if sy-subrc <> 0.

exit.

endif.

append itab.

clear itab.

endloop.

close dataset <name>.

loop at itab.

itab1-f1 = itab+0(4).

itab1-f2 = itab+4(3).

itab1-f3 = itab+7(5).

append itab1.

clear itab1.

endloop.

Sreedhar

Read only

Former Member
0 Likes
577

FORM read_file.

data : BEGIN OF IT_LINE ,

VBELN(10) TYPE C,

BOLNR(35) TYPE C,

TRAID(20) TYPE C,

CARR_CHARGES(20) TYPE C,

CURR_CODE(3) TYPE C,

ORD_CNT(4) TYPE C,

EXP_CNT(4) TYPE C,

WADAT_IST(10) TYPE C,

END OF IT_LINE.

DATA : lv_len TYPE i, " String length

OPEN DATASET p_filename FOR INPUT IN TEXT MODE

ENCODING DEFAULT

MESSAGE v_mess.

if sy-subrc <> 0.

exit.

endif.

*--file opened.

DO.

READ DATASET p_filename INTO it_line.

IF sy-subrc = 0.

APPEND it_line.

CLEAR it_line.

ELSE.

EXIT.

ENDIF.

ENDDO.

As your input file is a fixed length file(i mean each field will be of specific length always) you can define an internal table with those fields of specified length & if you use READ dataset, automatically those fields will be populated properly in the IT_LINE table.no need to take another internal table to sort out.

you can use 1 internal table to read the values from the file & the values will go and fit in the table fields.

regards

srikanth

added comments

Message was edited by: Srikanth Kidambi