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: 

Linefeed at end of file

flvia_s
Participant
0 Kudos

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

1 ACCEPTED SOLUTION

Tomas_Buryanek
Active Contributor
0 Kudos

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. ...

-- Tomas --
9 REPLIES 9

Tomas_Buryanek
Active Contributor
0 Kudos

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. ...

-- Tomas --

flvia_s
Participant
0 Kudos

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.

0 Kudos

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.

-- Tomas --

0 Kudos

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 Kudos

I would recommend using string templates.

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

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

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

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

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 '...'.

Sandra_Rossi
Active Contributor

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.

rejiomusic
Explorer
0 Kudos

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