How to add space(s) at the end of the file?
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:
- 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
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.
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!
hi, I have moved it to ABAP development space . But not sure how to publish this in General ABAP space.
Thanks for your comments!
Good Work.
I would like to update a similar thread, in which the ` ` is replaced by CL_ABAP_CONV_IN_CE=>UCCP( '00a0' )
Reference http://scn.sap.com/thread/1622863
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_file-export50 TO va_filename LENGHT 67.
Che
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
Hi Sudhir, i have tried this option. i.e empty_Space(40) is not adding space at the end of the file
Hi UMA ,
Try the third option ( Respecting blanks )given in this below link . Check this, hope will help you.
It's given with an example as per your requirement.
http://help.sap.com/abapdocu_70/en/ABAPCONCATENATE.htm
With Regards,
Sudhir S
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_file-export50 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.