Skip to Content

Linefeed at end of file

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

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

7 Answers

  • Best Answer
    Oct 11, 2016 at 07:40 AM

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

    Add comment
    10|10000 characters needed characters exceeded

  • Oct 12, 2016 at 07:00 AM

    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.

    Add comment
    10|10000 characters needed characters exceeded

  • 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

    Add comment
    10|10000 characters needed characters exceeded

  • Oct 11, 2016 at 02:50 PM

    I would recommend using string templates.

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

    Add comment
    10|10000 characters needed characters exceeded

  • Oct 11, 2016 at 02:32 PM

    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.
    Add comment
    10|10000 characters needed characters exceeded

  • Oct 11, 2016 at 07:46 PM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • Oct 12, 2016 at 06:43 AM

    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 '...'.
    Add comment
    10|10000 characters needed characters exceeded