Skip to Content
0

Linefeed at end of file

Oct 10, 2016 at 09:27 PM

264

avatar image

Hello experts.

I'm creating an output file and I need it to have a LF, but not CR at the end of file. Each file line has 500 characters and I need to assign LF to the position +499(1).

I have tried to assign x_file+499(1) = CL_ABAP_CHAR_UTILITIES=>NEWLINE, but it doesn't work. The LF appears next to the last written character.Example: COMPANY20161010WERKSLF (not what I want).

Here is my statment to write the file to a UNIX system:

OPEN DATASET v_filename FOR OUTPUT IN TEXT MODE ENCODING DEFAULT WITH NATIVE LINEFEED.

What would you guys suggest?
Best regards,

Alm

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

7 Answers

Best Answer
Tomas Buryanek Oct 11, 2016 at 07:40 AM
0

Try move "file line" (which I believe is defined as CHAR length 500) to string before TRANSFER. And transfer that string.

Quote from ABAP keyword help (DATASET IN TEXT MODE):

... If the data type is character-like and flat, any trailing blanks are truncated. In the data type string, trailing blanks are not truncated. ...

Share
10 |10000 characters needed characters left characters exceeded
Sandra Rossi Oct 12, 2016 at 07:00 AM
2

If your question is "I need to have lines with each the exact length 500" (trailing blanks), considering that X_LINE is a character-type field (does it?), then you may use:

TRANSFER x_line TO v_filename LENGTH 500.

You don't need to use addition WITH NATIVE LINEFEED. By default, it will place the LF character at the end of the line if your application server is Unix/Linux.

Of course, using LENGTH 500 means that the LF is written at position x_line+500(1) (i.e. not at x_line+499(1) what you're asking for).

If you need the characters in a specific code page other than UTF-8, please tell us.

Share
10 |10000 characters needed characters left characters exceeded
Reji Prabhakaran Oct 14, 2016 at 02:44 PM
-1

hey Alm,

CL_ABAP_CHAR_UTILITIES has an attribute CR_LF which has a value ##. The first hash has the hexadecimal value of CR and the later has the value for LF. Try using the second half by CL_ABAP_CHAR_UTILITIES=>CR_LF+1(1) and see if that gives you the right line feed at the end of the file.

Let us know if that works! Happy Weekend!

Thanks,

Reji

Share
10 |10000 characters needed characters left characters exceeded
Andre Schüßler Oct 11, 2016 at 02:50 PM
0

I would recommend using string templates.

data(text) = 'HELLO'.
data(text_with_newline) = |{ text WIDTH = 499 }\n|.

Share
10 |10000 characters needed characters left characters exceeded
Alm s Oct 11, 2016 at 02:32 PM
0

Hi Tomas. Thanks a lot for your help. I think I have implemented your suggestion correctly, but still it is not working.

  data: v_filename  type string,
        w_fileconc  type c length 500,
        w_transfer  type string,
        x_fileline  type tp_file.

  v_filename = p_dir.

  OPEN DATASET v_filename FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.

    loop at t_file into x_fileline.
      w_fileconc        = x_fileline-rec.
      w_fileconc+499(1) = CL_ABAP_CHAR_UTILITIES=>newline.
      concatenate w_transfer w_fileconc into w_transfer.
      clear w_fileconc.
    endloop.

    TRANSFER w_transfer to v_filename.
    CLOSE DATASET v_filename.
Show 2 Share
10 |10000 characters needed characters left characters exceeded

Not sure where is the problem with DATASET. I tried the same with cl_gui_frontend_services=>gui_download (ASC mode) and it worked - file had "newlines" exactly at +499(1) positions.

I forgot to add that you can also experiment with:

TRANSFER file NO END OF LINE. "without NO END OF LINE you get CRLF forcefuly

Or try BINARY MODE data transfer as Horst suggested.

0

What are you doing precisely to check that it is not working ? Do you download the AL11 file to your PC and then open it with Notepad++ for example ?

0
Horst Keller
Oct 11, 2016 at 07:46 PM
0

Hmm,

The concatenation should work as shown. Or doesn't it? (Where I'd prefer string templates too).

Having the internal table lines including the LF in the string, you might use CL_ABAP_CODEPAGE to convert the string to UTF8 and transfer the resulting XSTRING to the file in binary mode. That's the recommended way anyhow.

Horst

Share
10 |10000 characters needed characters left characters exceeded
Horst Keller
Oct 12, 2016 at 06:43 AM
0

PS: The idea is

DATA text1 TYPE c LENGTH 10 VALUE 'x'.
DATA text2 TYPE c LENGTH 10 VALUE 'y'.
DATA result TYPE string.
DATA binary TYPE xstring.

text1+9(1) = |\n|.
text2+9(1) = |\n|.

CONCATENATE text1 text2 INTO result.

binary = cl_abap_codepage=>convert_to( result ).

OPEN DATASET '...' FOR OUTPUT IN BINARY MODE.
TRANSFER binary TO '...'.
Share
10 |10000 characters needed characters left characters exceeded