Friday, July 11, 2008

Keep trailing spaces in a file

By Joyjit Ghosh,
Kolkata, India.

In few requirements I have seen that client wants to keep the trailing spaces in the file that is generated from an interface program/data extraction program. This is important for the legacy application that ultimately processes this file.

This tip will show us how to keep trailing blanks in a file.

As of Web AS 2.0 we cannot use CONCATENATE statement as it ignores trailing spaces but from release 7.0 this statement can be used with new addition RESPECTING BLANKS to achieve the same. So to accomplish this task in 6.20 we can use the method UCCP of class CL_ABAP_CONV_IN_CE.

REPORT z_sn_space.

CONSTANTS: c_space TYPE syhex02 VALUE '00a0'. " Value for space

DATA: g_space TYPE string,

l_pos type i.

TYPES: BEGIN OF ty_data,

field1(8) TYPE c,

END OF ty_data.

DATA: wa_data TYPE ty_data.

DATA: i_data TYPE STANDARD TABLE OF ty_data.

* Get the actual value for a space

g_space = cl_abap_conv_in_ce=>uccp( c_space ).

wa_data-field1 = 'Joy'.

L_pos = strlen( wa_data-field1 ).

Do 5 times.

wa_data-field1+l_pos(1) = g_space.

L_pos = l_pos + 1.

Enddo.

APPEND wa_data TO i_data.

CALL METHOD cl_gui_frontend_services=>gui_download

EXPORTING

filename = 'C:\trailing_space_demo.txt'

CHANGING

data_tab = i_data

EXCEPTIONS

FILE_WRITE_ERROR = 1

NO_BATCH = 2

GUI_REFUSE_FILETRANSFER = 3

INVALID_TYPE = 4

NO_AUTHORITY = 5

UNKNOWN_ERROR = 6

HEADER_NOT_ALLOWED = 7

SEPARATOR_NOT_ALLOWED = 8

FILESIZE_NOT_ALLOWED = 9

HEADER_TOO_LONG = 10

DP_ERROR_CREATE = 11

DP_ERROR_SEND = 12

DP_ERROR_WRITE = 13

UNKNOWN_DP_ERROR = 14

ACCESS_DENIED = 15

DP_OUT_OF_MEMORY = 16

DISK_FULL = 17

DP_TIMEOUT = 18

FILE_NOT_FOUND = 19

DATAPROVIDER_EXCEPTION = 20

CONTROL_FLUSH_ERROR = 21

NOT_SUPPORTED_BY_GUI = 22

ERROR_NO_GUI = 23

others = 24

.

IF sy-subrc <> 0.

* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.


Output of the generated file:

No comments: