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

reading data from application server to internal table.

Former Member
0 Likes
3,199

Hi ,

I am tryng to read data from application server to my internal table.

I put the code like this ..

my internal table

TYPES: BEGIN OF T_ IDNO,

IDNO(10) TYPE C,

END OF T_IDNO.

DATA: LT_IDNO TYPE TABLE OF T_IDNO,

LS_IDNO LIKE LINE OF LT_IDNO.

OPEN DATASET P_FILE FOR INPUT IN TEXT MODE ENCODING DEFAULT.

IF SY-SUBRC EQ 0.

READ DATASET P_FILE INTO ....?

how to read this data into my internal table.

can any one please help me.

Thanks.

3 REPLIES 3
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
760
TYPES: BEGIN OF T_ IDNO,
IDNO(10) TYPE C,
END OF T_IDNO.

DATA: LT_IDNO TYPE TABLE OF T_IDNO,
LS_IDNO LIKE LINE OF LT_IDNO.


OPEN DATASET P_FILE FOR INPUT IN TEXT MODE ENCODING DEFAULT.
IF SY-SUBRC EQ 0.
READ DATASET P_FILE INTO ls_idno.
  if sy-subrc = 0.
    append ls_idno to lt_idno.
  endif.
endif.

Regards,

Rich Heilman

Read only

Former Member
0 Likes
760

Hi,

To read data from a file on the application server, use the READ DATASET statement:

Syntax

READ DATASET <dsn> INTO <f> [LENGTH <len>].

This statement reads data from the file <dsn> into the variable <f>. In order to determine into which variable you should read data from a file, you need to know the structure of the file.

You can specify the transfer mode in the OPEN DATASET statement. If you have not already opened the file for reading, 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.

If the system was able to read data successfully, SY-SUBRC is set to 0. When the end of the file is reached, SY-SUBRC is set to 4. If the file could not be opened, SY-SUBRC is set to 8.

If you are working in binary mode, you can use the LENGTH addition to find out the length of the data transferred to <f>. The system sets the value of the variable <len> to this length.

DATA FNAME(60) VALUE 'myfile'.

DATA: TEXT1(12) VALUE 'abcdefghijkl',

TEXT2(5),

LENG 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 LENG.

WRITE: / SY-SUBRC, TEXT2, LENG.

IF SY-SUBRC <> 0.

EXIT.

ENDIF.

ENDDO.

CLOSE DATASET FNAME.

The output is:

0 abcde 5

0 fghij 5

4 kl### 2

This example fills the file "myfile" with 12 bytes from the field TEXT1. It is then read into the field TEXT2 in 5-byte portions. Note here that the system fills up the last three bytes of TEXT2 with zeros after the end of the file has been reached. The number of bytes transferred is contained in the field LENG.

If you are working in text mode, you can use the LENGTH addition to find out the length of the current line in the file. The system sets the value of the variable <len> to the length of the line. The system calculates this by counting the number of bytes between the current position and the next end of line marker in the file.

DATA FNAME(60) VALUE 'myfile'.

DATA: TEXT1(4) VALUE '1234 ',

TEXT2(8) VALUE '12345678',

TEXT3(2),

LENG TYPE I.

OPEN DATASET FNAME FOR OUTPUT IN TEXT MODE.

TRANSFER: TEXT1 TO FNAME,

TEXT2 TO FNAME.

CLOSE DATASET FNAME.

OPEN DATASET FNAME FOR INPUT IN TEXT MODE.

DO 2 TIMES.

READ DATASET FNAME INTO TEXT3 LENGTH LENG.

WRITE: / TEXT3, LENG.

ENDDO.

CLOSE DATASET FNAME.

The output appears as follows:

12 4

12 8

This example writes the strings TEXT1 and TEXT2 to the file "myfile" in text mode. They are then read into the string TEXT3 (length 2). The amount of memory occupied by the lines is read into the field LENG.

<b>Note</b> In your case you can transfer the records into your Internal table directly, if the structure of the file is same as the internal table.

Regards,

Vara

Read only

Former Member
0 Likes
760

TYPES: BEGIN OF T_ IDNO,

IDNO(10) TYPE C,

END OF T_IDNO.

DATA: LT_IDNO TYPE TABLE OF T_IDNO,

LS_IDNO LIKE LINE OF LT_IDNO.

FIELD-SYMBOLS : <fs> TYPE ANY.

OPEN DATASET P_FILE FOR INPUT IN TEXT MODE ENCODING DEFAULT.

IF SY-SUBRC EQ 0.

READ DATASET P_FILE INTO <fs>.

IF SY-SUBRC = 0.

APPEND <fs> TO T_IDNO.

ENDIF.

OR

OPEN DATASET P_FILE FOR INPUT IN TEXT MODE ENCODING DEFAULT.

IF SY-SUBRC EQ 0.

READ DATASET P_FILE INTO LS_IDNO-fieldname.

IF SY-SUBRC = 0.

APPEND LS_IDNO TO T_IDNO.

ENDIF.

Reward points if the answer is helpful.

Regards,

Mukul