Skip to Content

How to add space(s) at the end of the file?

I had a challenge in my project “To generate a fixed length file with spaces at the end which is send to the application server through an ABAP program”.

This is a very simple one which we may not know unless we come across particular issues. Hence I would like to share this to everyone so that we can save time if we face similar issues in future.

The total length of the file is 67 characters. (Fixed length).

First 50 characters are filled with some values remaining 17 characters should be filled with spaces.

As we all know to send a file to application server, following ABAP statements can be used

OPEN DATASET

TRANSFER <data> to <file>

CLOSE DATASET.

Following precondition to be considered:

  1. Instead of type C use STRINGS and add spaces until they have the appropriate length.
  2. i.e. File name, Variable used to transfer the data to the file must be of type String.
  3. Before transferring the data to the application server, generally we use concatenate statement to combine all values in the work area / variables.
  4. i.e. In this case the fields we use to combine must be a character-type data object (data type C, N, D, T, or STRING)”
  5. How to append spaces at the end of the file ?  Instead of normal single quotes ‘ ’

   USE quote like this  ` `   (Back quote or open quote)i.e. Press key before numeric 1. 

<SAMPLE CODE >

Data:lv_fixed type string,

             va_filename type string.

*

     OPEN DATASET va_filename FOR OUTPUT IN TEXTMODE ENCODING DEFAULT.

          LOOP AT it_fixed_file INTO wa_fixed_file.

                            MOVE wa_fixed_fileexport50 TO lv_fixed.  “First 50 characters moved    

           WHILE strlen( lv_fixed ) < 67.            “Check the length    

             CONCATENATE  lv_fixed ` ` INTO lv_fixed. ”Add spaces at the end    

           ENDWHILE.  

         TRANSFER lv_fixed TO va_filename.  “Transfer file to application server

        ENDLOOP.

   CLOSE DATASET va_filename.

After executing the above sample code, file is sent to the application server. File is downloaded as a text file and I tried to view the file.

But I couldn’t see the spaces at the end of the file. Ultra Editor is used to view the contents of the file.

How to solve this space issue?

There is an additional option in OPEN DATASET, to retain the spaces in the file after the download.

… WITH WINDOWS LINEFEED

Effect

The line end marker is set to “CRLF” regardless of the access type and operating system of the application server.

The line end marker is interpreted in accordance with the current code page. If a code page is specified explicitly using the addition CODE PAGE, the characters of the line end marker must be available or be written according to this code page.

Note

The addition WITH WINDOWS LINEFEED is intended for use with MS Windows files in which the specific line end marker is to be retained, even if the operating system of the current application server is UNIX, OS/390, or IBM i5/OS (previously known as OS/400).

Then I changed the code and got the desired output :

OPEN DATASET va_filename FOR OUTPUT IN TEXTMODE

         ENCODING DEFAULT WITH WINDOWS LINEFED.

Output of the file in application server. After the value 3135  17 spaces are added at the end of the file and end of the file is marked as #.

Outfile_with_spaces.png

Download the file from application server and open the file in ULTRA EDITOR to check the spaces added at the end of the file.

Spaces are identified as Dots (.) here.

File_Ultraedit.png

To report this post you need to login first.

8 Comments

You must be Logged on to comment or reply to a post.

  1. Jelena Perfiljeva

    Good post, you might want to “cross-publish” it to General ABAP “space”.

    I remember back in the mainframe days we used to solve this by adding an extra field filled with ‘X’ at the end of every line. Good to know the other options. Thanks!

    (0) 
    1. Uma Arjunan Post author

      hi, I have moved it to ABAP development space . But not sure how to publish this in General ABAP space.

      Thanks for your comments!

      (0) 
  2. Che Eky

    A fixed length file can be produced using the command TRANSFER with addition LENGHT:

    TRANSFER dobj TO dset [LENGTH len]
                          [NO END OF LINE].

    TRANSFER wa_fixed_fileexport50 TO va_filename LENGHT 67.

    Che

    (0) 
  3. Sudhir S

    Hi Uma ,

        Try like this.

    data : empty_space(40).

    CONCATENATE  lv_fixed ` ` INTO lv_fixed.

    condense lv_fixed no-gaps.

    CONCATENATE  lv_fixed empty_space INTO lv_fixed.

    —————

    Hope this will be helpful for you.

    With Regards,

    Sudhir S



    (0) 
        1. Uma Arjunan Post author

          data : lv_empty_space(17) type c.

                   lv_fixed type string

          1.   Concatenate lv_fixed lv_empty_space into lv_fixed RESPECTING BLANKS .

          2. TRANSFER wa_fixed_fileexport50 TO va_filename LENGTH 67.

          Both options are working and adding spaces at the end. With this option we can avoid using While – Endwhile to add spaces.

          … RESPECTING BLANKS

          Effect

          The addition RESPECTING BLANKS is only allowed during string processing and causes the closing spaces for data objects dobj1 dobj2 … or rows in the internal table itab to be taken into account. Without the addon, this is only the case with string.

          Note

          With addition RESPECTING BLANKS, statement CONCATENATE can be used in order to assign any character strings EX>text – taking into account the closing empty character – to target str of type string: CLEAR str. CONCATENATE str text INTO str RESPECTING BLANKS.

          But Windows line feed is also necessary if the space has to be retained after the download.

          Thanks sudhir & Che for providing the other options.

          (0) 

Leave a Reply