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

Problem in Open Dataset and Read Data set

Former Member
0 Likes
2,397

Hi All,

I want to open a file and read it's contents line by line but it only reads one line and then comes out of loop .

For eg :-

OPEN DATASET p_v FOR INPUT IN TEXT MODE ENCODING DEFAULT .

IF sy-subrc eq 0.

DO.

READ DATASET p_v INTO test LENGTH leng.

IF sy-subrc eq 0.

else .

EXIT.

ENDIF.

ENDDO.

ENDIF.

CLOSE DATASET p_v.

Any suggesions welcome .

Thanks

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,116

Hi

See the sample code where the OPENDATASET is used and do accordingly

ABAP code for uploading a TAB delimited file into an internal table. See code below for structures.

&----


*& Report ZUPLOADTAB *

*& *

&----


*& Example of Uploading tab delimited file *

*& *

&----


REPORT zuploadtab .

PARAMETERS: p_infile LIKE rlgrap-filename

OBLIGATORY DEFAULT '/usr/sap/'..

DATA: ld_file LIKE rlgrap-filename.

*Internal tabe to store upload data

TYPES: BEGIN OF t_record,

name1 like pa0002-VORNA,

name2 like pa0002-name2,

age type i,

END OF t_record.

DATA: it_record TYPE STANDARD TABLE OF t_record INITIAL SIZE 0,

wa_record TYPE t_record.

*Text version of data table

TYPES: begin of t_uploadtxt,

name1(10) type c,

name2(15) type c,

age(5) type c,

end of t_uploadtxt.

DATA: wa_uploadtxt TYPE t_uploadtxt.

*String value to data in initially.

DATA: wa_string(255) type c.

constants: con_tab TYPE x VALUE '09'.

*If you have Unicode check active in program attributes then you will

*need to declare constants as follows:

*class cl_abap_char_utilities definition load.

*constants:

  • con_tab type c value cl_abap_char_utilities=>HORIZONTAL_TAB.

************************************************************************

*START-OF-SELECTION

START-OF-SELECTION.

ld_file = p_infile.

OPEN DATASET ld_file FOR INPUT IN TEXT MODE ENCODING DEFAULT.

IF sy-subrc NE 0.

ELSE.

DO.

CLEAR: wa_string, wa_uploadtxt.

READ DATASET ld_file INTO wa_string.

IF sy-subrc NE 0.

EXIT.

ELSE.

SPLIT wa_string AT con_tab INTO wa_uploadtxt-name1

wa_uploadtxt-name2

wa_uploadtxt-age.

MOVE-CORRESPONDING wa_uploadtxt TO wa_upload.

APPEND wa_upload TO it_record.

ENDIF.

ENDDO.

CLOSE DATASET ld_file.

ENDIF.

************************************************************************

*END-OF-SELECTION

END-OF-SELECTION.

*!! Text data is now contained within the internal table IT_RECORD

  • Display report data for illustration purposes

loop at it_record into wa_record.

write:/ sy-vline,

(10) wa_record-name1, sy-vline,

(10) wa_record-name2, sy-vline,

(10) wa_record-age, sy-vline.

endloop.

Reward if useful

regards

Anji

Hi All,

I want to open a file and read it's contents line by line but it only reads one line and then comes out of loop .

For eg :-

OPEN DATASET p_v FOR INPUT IN TEXT MODE ENCODING DEFAULT .

IF sy-subrc eq 0.

DO.

READ DATASET p_v INTO test LENGTH leng.

IF sy-subrc eq 0.

else .

EXIT.

ENDIF.

ENDDO.

ENDIF.

CLOSE DATASET p_v.

Any suggesions welcome .

Thanks

9 REPLIES 9
Read only

Former Member
0 Likes
2,117

Hi

See the sample code where the OPENDATASET is used and do accordingly

ABAP code for uploading a TAB delimited file into an internal table. See code below for structures.

&----


*& Report ZUPLOADTAB *

*& *

&----


*& Example of Uploading tab delimited file *

*& *

&----


REPORT zuploadtab .

PARAMETERS: p_infile LIKE rlgrap-filename

OBLIGATORY DEFAULT '/usr/sap/'..

DATA: ld_file LIKE rlgrap-filename.

*Internal tabe to store upload data

TYPES: BEGIN OF t_record,

name1 like pa0002-VORNA,

name2 like pa0002-name2,

age type i,

END OF t_record.

DATA: it_record TYPE STANDARD TABLE OF t_record INITIAL SIZE 0,

wa_record TYPE t_record.

*Text version of data table

TYPES: begin of t_uploadtxt,

name1(10) type c,

name2(15) type c,

age(5) type c,

end of t_uploadtxt.

DATA: wa_uploadtxt TYPE t_uploadtxt.

*String value to data in initially.

DATA: wa_string(255) type c.

constants: con_tab TYPE x VALUE '09'.

*If you have Unicode check active in program attributes then you will

*need to declare constants as follows:

*class cl_abap_char_utilities definition load.

*constants:

  • con_tab type c value cl_abap_char_utilities=>HORIZONTAL_TAB.

************************************************************************

*START-OF-SELECTION

START-OF-SELECTION.

ld_file = p_infile.

OPEN DATASET ld_file FOR INPUT IN TEXT MODE ENCODING DEFAULT.

IF sy-subrc NE 0.

ELSE.

DO.

CLEAR: wa_string, wa_uploadtxt.

READ DATASET ld_file INTO wa_string.

IF sy-subrc NE 0.

EXIT.

ELSE.

SPLIT wa_string AT con_tab INTO wa_uploadtxt-name1

wa_uploadtxt-name2

wa_uploadtxt-age.

MOVE-CORRESPONDING wa_uploadtxt TO wa_upload.

APPEND wa_upload TO it_record.

ENDIF.

ENDDO.

CLOSE DATASET ld_file.

ENDIF.

************************************************************************

*END-OF-SELECTION

END-OF-SELECTION.

*!! Text data is now contained within the internal table IT_RECORD

  • Display report data for illustration purposes

loop at it_record into wa_record.

write:/ sy-vline,

(10) wa_record-name1, sy-vline,

(10) wa_record-name2, sy-vline,

(10) wa_record-age, sy-vline.

endloop.

Reward if useful

regards

Anji

Read only

Former Member
0 Likes
2,116

OPEN DATASET p_v FOR INPUT IN TEXT MODE ENCODING DEFAULT .

IF sy-subrc eq 0.

DO.

READ DATASET p_v INTO test LENGTH leng.

IF sy-subrc eq 0.

<b>move leng to l_table.

append l_table.</b>

else .

EXIT.

ENDIF.

ENDDO.

ENDIF.

CLOSE DATASET p_v.

Read only

0 Likes
2,116

Thanks ,

Actually i have more than i records like

ABC 1

BBB 2

BNK 3

....

when i tried to read the file contents then it reads just first line contents i.e ABC 1 and comes out from loop .

Any suggestions welcome,

Thanks in Advance.

Read only

Former Member
0 Likes
2,116

Hi,

OPEN DATASET p_v FOR INPUT IN TEXT MODE ENCODING DEFAULT .

if sy-subrc EQ 0.

DO.

READ DATASET p_v INTO w_lines.

if sy-subrc EQ0.

<<splitt the record as per your requirement into fields of an int table>>

else.

exit

endif.

enddo.

endif.

Read only

Former Member
0 Likes
2,116

Hi Navdeep,

The string TEST will store only 1 record at a time. Hence in your case, it overwrites all the previous data on TEST & finally stores the last record.

You need to APPEND each line of record from TEST into a internal table.

Create an internal table which has 2 fields as per your file structure.

Move the data from TEST into your internal table & Append the internal table.

This will solve the problem.

Best regards,

Prashant

Read only

0 Likes
2,116

Thanks,

I use APPEND already but the dataset is not reading the whole file contents actually it contains too large data .

if any one have code that read logical file name from server and contents of that file then send me code .

Thanks in advance,

Read only

Former Member
0 Likes
2,116

Hi,

You need to append the data into internal table also after reading from the file:


DATA: it_test like table of test.

OPEN DATASET p_v FOR INPUT IN TEXT MODE ENCODING DEFAULT .
IF sy-subrc eq 0.
DO.
READ DATASET p_v INTO test LENGTH leng.
IF sy-subrc eq 0.
else .
Append test to it_test.
EXIT.
ENDIF.
ENDDO.
ENDIF.
CLOSE DATASET p_v.

Reward points if helpful.

Ashvender

Read only

former_member194152
Contributor
0 Likes
2,116

Hi,

Try this logic...

OPEN DATASET phy_name_out FOR INPUT IN TEXT MODE ENCODING DEFAULT

IF sy-subrc NE 0.

CLOSE DATASET phy_name_out.

ENDIF.

DO.

CLEAR lv_file_line.

READ DATASET phy_name_out INTO lv_file_line.

IF sy-subrc EQ 0.

lv_file_line+0(1) = ' '.

SHIFT lv_file_line LEFT DELETING LEADING space.

SPLIT lv_file_line AT cl_abap_char_utilities=>horizontal_tab INTO

wa_ctab-bukrs

wa_ctab-fkdat

wa_ctab-vbeln

wa_ctab-kunag

wa_ctab-zterm

wa_ctab-inco1

wa_ctab-inco2

wa_ctab-matnr

wa_ctab-fkimg

wa_ctab-vrkme

wa_ctab-netwr

wa_ctab-waerk

wa_ctab-fkart

wa_ctab-lgort

wa_ctab-charg

wa_ctab-vkorg

wa_ctab-vtweg

wa_ctab-spart

wa_ctab-kunnr

wa_ctab-gjahr.

APPEND wa_ctab TO ctab.

ELSE.

CLOSE DATASET phy_name_out.

EXIT.

ENDIF.

ENDDO.

Regards

Gagan

Read only

Former Member
0 Likes
2,116

Thanks all ,

Issue resolved it's due to file format .

Thanks again to all for help and points also rewarded .