Application Development 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: 

Formatting issue in writing text file by open dataset

Former Member
0 Kudos

Hi all,

I want to write a text file with this format:

Col1 | Col2 | Col3 | Col4

abcdef | 1 | 2005.02.01 | 144

bcd | 134 | 2005.07.01 | 24

How could I keep the content displayed in line with the header?

p.s. Currently I could only write the content like this:

Col1 | Col2 | Col3 | Col4

abcdef | 1 | 2005.02.01 | 144

bcd | 134 | 2005.07.01 | 24

6 REPLIES 6

Former Member
0 Kudos

Hi Macy,

There's one more thing that you need to know here. If any of the fields in a record of the internal table is

initial, then you will get some alignment problems for the separator (in this case, '|'). For example, if your

internal table has 3 columns and the following entries,

01	ONE	SUNDAY	
02	TWO	MONDAY
03		TUESDAY
04	FOUR	WEDNESDAY
05	FIVE	THURSDAY
06	SIX	FRIDAY
07		SATURDAY

then your output in the file is going to look like this -

01|ONE|SUNDAY	
02|TWO|MONDAY
03||TUESDAY
04|FOUR|WEDNESDAY
05|FIVE|THURSDAY
06|SIX|FRIDAY
07||SATURDAY

Clearly, this is undesirable. As you can see above, this problem occurs even when the length of the content of the

fields in the internal table is uneven.

So I suggest that you do something like this -

DATA: BEGIN OF ITAB OCCURS 0,
        CARRID LIKE SFLIGHT-CARRID,
        SEPARATOR1,
        CONNID LIKE SFLIGHT-CONNID,
        SEPARATOR2,
        FLDATE LIKE SFLIGHT-FLDATE,
      END OF ITAB.
 
DATA : HEADER(20) VALUE 'SPFLI DETAILS',
       FILENAME(60) VALUE <give the name of the AppServer file here>,
       FILE_LINE(40).
 
 
SELECT CARRID
       CONNID
       FLDATE
  FROM SFLIGHT
  INTO CORRESPONDING FIELDS OF TABLE ITAB.
 
ITAB-SEPARATOR1 = '|'.
ITAB-SEPARATOR2 = '|'.
 
MODIFY ITAB TRANSPORTING SEPARATOR1 
                         SEPARATOR2 
                   WHERE SEPARATOR1 EQ SPACE.
 
 
LOOP AT ITAB.
  AT FIRST.
    OPEN DATASET FILENAME FOR OUTPUT IN TEXT MODE.
    TRANSFER HEADER TO FILENAME.
  ENDAT.
  TRANSFER ITAB TO FILENAME.
ENDLOOP.

Hope that make the point clear. If yes, I wouldn't mind more points :-). Otherwise, please do get back. And I'll

try to help you understand.

By the way, I already gave this same answer to your other question:-)

Regards,

Anand Mandalika.

Former Member
0 Kudos

Hi,

I forgot to address the header line issue in my earlier reply :-(. Here's what you can do.

In your example, the text 'COL2' is 4 characters long. But the values that come in the 2nd column of the internal table are always less than 4 characters long. So, you can declare the length of the 2nd column to be 4 characters, even if the values are always going to be less than that. Then, you an do something like this -

DATA: BEGIN OF ITAB OCCURS 0,
        CARRID(6),
        SEPARATOR1,
        CONNID(6),
        SEPARATOR2,
        FLDATE TYPE D,
      END OF ITAB.

DATA : FILENAME(60)VALUE <GIVE YOUE APPSERVER fILENAME>,
       FILE_LINE(40).


SELECT CARRID
       CONNID
       FLDATE
  FROM SFLIGHT
  INTO CORRESPONDING FIELDS OF TABLE ITAB.

ITAB-SEPARATOR1 = '|'.
ITAB-SEPARATOR2 = '|'.
ITAB-CARRID = 'CARRID'.
ITAB-CONNID = 'CONNID'.
ITAB-FLDATE = 'FLDATE'.

MODIFY ITAB TRANSPORTING SEPARATOR1
                         SEPARATOR2
                   WHERE SEPARATOR1 EQ SPACE.



IF NOT ITAB[] IS INITIAL.
  OPEN DATASET FILENAME FOR OUTPUT IN TEXT MODE.
  TRANSFER itab TO FILENAME.
ENDIF.


LOOP AT ITAB.
  TRANSFER ITAB TO FILENAME.
ENDLOOP.

Hope that helps,

Regards,

Anand Mandalika.

0 Kudos

Thanks again for your reply.

But it seems not working.

The problem is to make the field length fixed irrespective of its contents' length.

Any ideas?

0 Kudos

Hi Macy,

1. There should be a separator column between every two fields.

2. The length of the header need not actually depend on the field length always. but in some cases where you have a data field or a time field for example, the output length of the field will be a little more than the actual field length. A data field will need only 8 character, but if you want the separators to be displayed, then it will become 10 characters. similarly with the time field.

3. But in case you do not have any such fields, you can manage to get the data with proper alignment. you only have to adjust the name you give for the column header to fit to the length of the field.

Regards,

Anand Mandalika.

0 Kudos

Hi,

The case is the field length for each column is fixed, so the problem is how to add sufficient space ' ' to the field contents for each entry according to their actual lengths.

0 Kudos

Hi Macy,

I think it best if you mentioned the columns that you are actually using. Can you give the structure of your internal table ?

Regards,

Anand Mandalika.