Skip to Content
-2

How to set CR (Carriage Return) at end of line in ABAP?

May 03 at 03:49 AM

197

avatar image

Hi experts,

We are creating a flat file on the application server using 'open dataset'.

I need to place a CR at the end of each line.

I tried the below syntax, but it doesn't give a CR at end of line.

OPEN DATASET p_file FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
CONCATENATE l_data cl_abap_char_utilities=>cr_lf(1) INTO l_data.
TRANSFER l_data  TO p_file.

Please suggest how to set CR (Carriage Return)?

Thanks for your help in advance.

Lucy

10 |10000 characters needed characters left characters exceeded

Why do you believe your code doesn't add a CR at the end of the line? What's the evidence?

Have you tested in debugger, and seen whether x000D is on the end of l_data just before the transfer? (It may be visible as x0D00).

1

Are you actually looking for CR (carriage return, \r, move back to begin of line) and not LF (line feed, move down one line)

If you actually want to handle those separators, add the NO END OF LINE option in your transfer statement to prevent the append of a end of line separator in text mode.

2

Dear all,

Thank you for your suggestions.

I tried them all, but unfortunately, none is working.

Seems like it is not possible to achieve CR in TEXT MODE.

0

It is possible. You have problem somewhere else.

Definition of l_data, not using NO END OF LINE, wrongly downloading file from application server...

1
Tomas Buryanek

I did try NO END OF LINE, but all it does is writing all the rows of the file in a single line.

if I define any component of l_data as string, then it doesn't let me use TRANSFER DATASET.

Which method other than CG3Y do you suggest for downloading?

0

Of course CR does not create new line in most windows editors/viewers. CR+LF is for creating new line, but you want CR at the end of each "data set" ( ~ line), don't you?CG3Y binary download is OK for testing.

0
* Please Login or Register to Answer, Follow or Comment.

5 Answers

Best Answer
Tomas Buryanek May 03 at 12:01 PM
2

It looks like you need to use NO END OF LINE addition to TRANSFER command (because you are adding it yourself) ?

Show 2 Share
10 |10000 characters needed characters left characters exceeded

Hi Raymond Giuseppi & Tomas Buryanek ,

Thank you very much for prompting me to give NO END OF LINE a second try.

The first time I had tried it, I couldn't download the file due to some technical issues, and I just had a look at its contents in AL11.

By looking at the contents in AL11, I had presumed that the layout has been completely distorted.

But just now I tried NO END OF LINE once again, and after downloading the file & opening it in sakura editor, I saw CR!!!

Thanks a lot everybody for your valuable time & answers!

Below is a simplified version of the code I used, just for reference.

OPEN DATASET p_file FOR OUTPUT IN TEXT MODE ENCODING UTF-8 WITH BYTE-ORDER MARK.

        CONCATENATE wa_data cl_abap_char_utilities=>cr_lf(1) INTO wa_data.
        TRANSFER wa_data TO p_file NO END OF LINE.

   CLOSE DATASET p_file.
1

That looks rather outdated. Why do you clumsily use a class instead of using the corresponding string template directly? Why do you use CONCATENATE instead of string expressions?

1
Horst Keller
May 03 at 05:57 PM
2
ASSERT 
cl_abap_char_utilities=>newline
 = |\n|. 
ASSERT 
cl_abap_char_utilities=>horizontal_tab
 = |\t|. 
ASSERT 
cl_abap_char_utilities=>cr_lf
 = |\r\n|.

Share
10 |10000 characters needed characters left characters exceeded
Raymond Giuseppi May 03 at 11:02 AM
0

Is l_data a char or a string field, in first case the last character would be a space and not CR, CR would be the last non space character.

Show 2 Share
10 |10000 characters needed characters left characters exceeded

Hi Raymond,

Thanks for your comment.

l_data is just a workarea with various fields. The actual source code is having a loop on an internal table with l_data

as the work area. I didn't mention the loop for the sake of simplicity.

Could you please share how to set CR at end of line?

Just for your reference, below two fields are there inside the workarea.

BEGIN OF ty_main,
       saknr TYPE saknr,      "G/L Account Number
       txt20(48) TYPE c,      "G/L account short text
       END   OF ty_main,
0

Either change txt20 to string type or add a dummy field at end of structure to contain the CR character.

0
Rajkumar Narasimman May 03 at 05:31 AM
0

Hi Lucy,

The length of CR_LF is 2 charcters '##', Why are you using passing single character cl_abap_char_utilities=>cr_lf(1) instead of cl_abap_char_utilities=>cr_lf. Try below and check it.

CONCATENATE l_data cl_abap_char_utilities=>cr_lf INTO l_data.

Regards

Rajkumar Narasimman

Show 5 Share
10 |10000 characters needed characters left characters exceeded

Presumably the logic is that if cr_lf (carriage return linefeed) is two characters, and only CR is needed, then half of cr_lf (the first character) will yield it.

Also, the value of CR_LF is not ##. Try looking at the hex in the debugger.

2

Yes noted now, just saw the value(##) which we usually do, missed to check Hexadecimal(0D000A00). Thank you Matthew Billingham :)

1

Thank you Rajkumar Narasimman & Matthew Billingham for your comments.

As Matthew already said, I need only CR, that's why I am concatenating only half of cl_abap_char_utilities=>cr_lf.

But still I'm not getting CR :(

0

See my comment below your question.

Why do you believe your code doesn't add a CR at the end of the line? What's the evidence?

Have you tested in debugger, and seen whether x000D is on the end of l_data just before the transfer? (It may be visible as x0D00).

0

I am checking the output file by downloading it using CG3Y, and then opening it in 'Sakura editor'.

Please see the attached images.

file.jpg (84.1 kB)
crlf.jpg (64.9 kB)
0
Amy King May 03 at 01:24 PM
0

There's a solution noted by the OP in a discussion from 2013, CL_ABAP_CHAR_UTILITIES=cr_lf - how to achieve just 'CR'?

.

Share
10 |10000 characters needed characters left characters exceeded