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
TRANSFER <data> to <file>
Following precondition to be considered:
- Instead of type C use STRINGS and add spaces until they have the appropriate length.
- i.e. File name, Variable used to transfer the data to the file must be of type String.
- Before transferring the data to the application server, generally we use concatenate statement to combine all values in the work area / variables.
- 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)”
- 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_file–export50 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
TRANSFER lv_fixed TO va_filename. “Transfer file to application server
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
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.
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 #.
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.