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

read file in a different structure

Former Member
0 Likes
1,059

Hello,

I have two different files, which I want to read in a structure one after another. Later I have to distinguish which entry is from which file.

First I thought I can use function READ DATASET dsn INTO f for that purpose. But it seems that you have to use the same structures in this case, but then I have no differentiating factor within my structure.

I think there must be an easy way to do what. Unfortanately, I have not found something so far.

Does anyone has an idea? Thanks.

Regards,

Tobias

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
951

Hi

U can use different structure as workarea for reading file, anyway you know which kind file you're reading in order to know which structure has to be used.

Usually there's a symbol or a sign at the beginning of the file in order to define the kind of file

* Open file:
OPEN DATASET <FILE> ..........
IF SY-SUBRC = 0.
* Check first record
READ DATASET <FILE> INTO F.
* Suppose the first four characters indicate the kind of file
IF F(4) = 'HEAD'.
   FL_KIND = 'H'. "<-------Header
ELSE.
   FL_KIND = 'I'.  "<--------Item
ENDIF. 
IF FL_KIND = 'H'.
  H = F.
ELSE.
  I = F.
ENDIF.
* Read the next record
DO.
  READ DATASET <FILE> INTO F.
  IF FL_KIND = 'H'.
    H = F.
  ELSE.
    I = F.
  ENDIF.
ENDDO.

This is only a sample we should know how your files are and when u read them.

Max

Hello,

I have two different files, which I want to read in a structure one after another. Later I have to distinguish which entry is from which file.

First I thought I can use function READ DATASET dsn INTO f for that purpose. But it seems that you have to use the same structures in this case, but then I have no differentiating factor within my structure.

I think there must be an easy way to do what. Unfortanately, I have not found something so far.

Does anyone has an idea? Thanks.

Regards,

Tobias

6 REPLIES 6
Read only

MaryM
Participant
0 Likes
951

hi Tobias,

if you have to differentiate later you entries anyway, why cant you use 2 different structures at first.

read datased 1 into f1

read dataset 2 into f2

and now you can move them to internal tables or structures with some differentiating flag.

Regards,

MaryM

Read only

Former Member
0 Likes
952

Hi

U can use different structure as workarea for reading file, anyway you know which kind file you're reading in order to know which structure has to be used.

Usually there's a symbol or a sign at the beginning of the file in order to define the kind of file

* Open file:
OPEN DATASET <FILE> ..........
IF SY-SUBRC = 0.
* Check first record
READ DATASET <FILE> INTO F.
* Suppose the first four characters indicate the kind of file
IF F(4) = 'HEAD'.
   FL_KIND = 'H'. "<-------Header
ELSE.
   FL_KIND = 'I'.  "<--------Item
ENDIF. 
IF FL_KIND = 'H'.
  H = F.
ELSE.
  I = F.
ENDIF.
* Read the next record
DO.
  READ DATASET <FILE> INTO F.
  IF FL_KIND = 'H'.
    H = F.
  ELSE.
    I = F.
  ENDIF.
ENDDO.

This is only a sample we should know how your files are and when u read them.

Max

Read only

0 Likes
951

OK, MaryM`s idea was simple and good. Thank you.

But anyway I am interested if it would be possible to read in two files, which have the following structure

DATE

ISO_CODE

RATE

NOTATION

into a structure (internal table):

DATA: BEGIN OF ITAB OCCURS 1000,

DAT TYPE D,

CUR LIKE TCURR-FCURR,

RATE(16) TYPE C,

NOT(1) TYPE C

FIL(1) TYPE C.

DATA: END OF ITAB.

with the following commands:

DO.

READ DATASET DAT_IN1 INTO ITAB.

move '1' to ITAB-FIL.

ENDDO.

DO.

READ DATASET DAT_IN2 INTO ITAB.

MOVE '2' to ITAB-FIL.

ENDDO.

Thanks again for your help.

Regards,

Tobias

Edited by: Tobias Meiler on Apr 17, 2009 1:58 PM

Read only

0 Likes
951

Hi,

that should be totally possible.

data: wa like line of itab.

DO.

READ DATASET DAT_IN1 INTO wa.

move '1' to wa-FIL.

append wa to itab.

ENDDO.

DO.

READ DATASET DAT_IN2 INTO ITAB.

MOVE '2' to wa-FIL.

append wa to itab.

ENDDO.

Let us know if it dosen't work.

Regards,

MaryM

Read only

Former Member
0 Likes
951

A part of the question is answered but not overall. There is a point I am still interested in.

Read only

Former Member
0 Likes
951

Hello,

What i believe is that for the solution u can add one more column in the structure say the Differentiator, and pass some value into it depending upon the file from which it is getting

for eg:

u can pass F1 if it is being read from file one

and F2 if it is being read from file two.

Then you can carry on with the calculation giving the conditions as

if Differentiator = F1

Do sme calculation

else

do the other calculation

Endif.

Regards

Edited by: Richa Wahi on Apr 17, 2009 2:13 PM