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:
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
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 #.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
37 | |
10 | |
6 | |
4 | |
3 | |
3 | |
3 | |
2 | |
2 | |
2 |