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: 

Add trailing spaces to a text

BTP_Architect
Participant
0 Kudos

Hello All,

I have an issue with concatenate instruction;

In fact, I want to concatenate some data in several lines, each line must have the length of 128 char.

To get those 128 char filled I should concatenate each time with a trailing space constante V_TRAIL

Then I should output those data to a file.


DATA: BEGIN OF IT_OUTB OCCURS 0,
    LINE(128) TYPE c,
END OF IT_OUTB.
DATA: V_TRAIL(60) TYPE C.
.
.
.
CONCATENATE V_1 V_2 V_TRAIL INTO IT_OUTB-LINE RESPECTING BLANKS.
APPEND IT_OUTB.
.
.
.
DATA: LV_DOS TYPE STRING.
.
.
.
OPEN DATASET LV_DOS FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
LOOP AT IT_OUTB.
    TRANSFER IT_OUTB TO LV_DOS LENGTH 128.
ENDLOOP.
CLOSE DATASET LV_DOS.

The problem is my output file is not generated with the trailing spaces => length of each line is less then 128.

Could you please help me.

Regards,

Moez.

1 ACCEPTED SOLUTION

kesavadas_thekkillath
Active Contributor
0 Kudos

You can try this


DATA: lv_space TYPE string.

lv_space = cl_abap_conv_in_ce=>uccp( '00a0' ).

OPEN DATASET LV_DOS FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
LOOP AT IT_OUTB.
    Lv_pos = strlen( IT_OUTB-line ).
    do 2 times.
    IT_OUTB+lv_pos(1) = lv_space.
    l_pos = lv_pos + 1.
    enddo.
    TRANSFER IT_OUTB TO LV_DOS.
ENDLOOP.
CLOSE DATASET LV_DOS.

11 REPLIES 11

rainer_hbenthal
Active Contributor
0 Kudos

Character variables always have spaces at their end and they will always be ignored. Please read my [blog|http://www.sdn.sap.com/irj/scn/weblogs?blog=/pub/wlg/15889] [original link is broken] [original link is broken] [original link is broken];. Instead using char variables use string variables. Or use LENGTH attribute in your transfer statement.

0 Kudos

Hi Rainer,

I already use the length parameter in my transfer instruction.

Regards,

Moez.

former_member182426
Active Contributor
0 Kudos

Hi,

Directly you are concatenating the blan spaces.

Your not checking the length of filed LINe is < 128 or >128.

for every line your concatenating....

Regards,

Shankar.

0 Kudos

Hi Shankar,

I know exactly the length of each field V_1 , V_2 ...

That's why I know exactly the length of each V_TRAIL in each line.

For the value of V_TRAIL I keep it initial (blank) as it's defined.

Regards,

Moez.

0 Kudos

To be honest its hard to believe that the length parameter will fail.

If the value of len is greater than the number of characters or bytes in dobj, hexadecimal 0 or blank characters are transferred to the file instead of the missing bytes or characters, depending on whether the file was opened as a (legacy) text file or a (legacy) binary file.

Try using string as output variable. Of course you need to make sure that the len is 128, for tesing i would like to insert a strlen( ) test.

0 Kudos

Rainer,

I will try with strings and feed you back.

Thanks,

Rgrds,

Moez.

SuhaSaha
Advisor
Advisor
0 Kudos

Hello,

I think you cannot achieve this using character literals instead use STRING literals (in that case you have to use CL_ABAP_CHAR_UTILITIES=>CR_LF or CL_ABAP_CHAR_UTILITIES=>NEWLINE to get the line break after every 128 characters)

Read Rainer's blog on Character v/s String Literals, this will help you to get concepts clear.

BR,

Suhas

kesavadas_thekkillath
Active Contributor
0 Kudos

You can try this


DATA: lv_space TYPE string.

lv_space = cl_abap_conv_in_ce=>uccp( '00a0' ).

OPEN DATASET LV_DOS FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
LOOP AT IT_OUTB.
    Lv_pos = strlen( IT_OUTB-line ).
    do 2 times.
    IT_OUTB+lv_pos(1) = lv_space.
    l_pos = lv_pos + 1.
    enddo.
    TRANSFER IT_OUTB TO LV_DOS.
ENDLOOP.
CLOSE DATASET LV_DOS.

0 Kudos

Thank you Keshav for your response.

I did some changes in your suggestion and it's working now:


OPEN DATASET LV_DOS FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
LV_SPACE = CL_ABAP_CONV_IN_CE=>UCCP( '00a0' ).
LOOP AT IT_OUTB.
   WRITE LV_SPACE TO IT_OUTB-LINE+127(1).
   TRANSFER IT_OUTB-LINE TO LV_DOS LENGTH 128.
ENDLOOP.
CLOSE DATASET LV_DOS.

Thanks for all.

venkat_o
Active Contributor
0 Kudos

Hi, <li> I believe it works if you change the below way. Instead of


CONCATENATE V_1 V_2 V_TRAIL INTO IT_OUTB-LINE RESPECTING BLANKS.
APPEND IT_OUTB.
Change to

IT_OUTB-LINE+0(63)    = v_1.  "Lets say V_1 has 63 char length
IT_OUTB-LINE+63(64)   = v_2.  "Lets say V_2 has 64 char length
IT_OUTB-LINE+127(63)  = space."Blank space
APPEND IT_OUTB.
Thanks Venkat.O

0 Kudos

You cannot realize SPACEs with character literals but can do so using string literals.

BR,

Suhas