Detecting Carriage Return & Line Feed From Incoming File

When reading a file which contains a “Carriage Return” using the function module ‘GUI_UPLOAD’, the function module ‘GUI_UPLOAD’ will display the carriage return with the symbol ‘#’.

For example:

Using the following code, we will try to read the content the file ‘text_file.txt’ (link) which contains a “Carriage Return” at the end of the lines.

Code:

CALL FUNCTION \'GUI_UPLOAD\'
EXPORTING
filename = \'C:\\Users\\theuser\\Desktop\\test_file.txt\'
TABLES
data_tab = gt_input_file
EXCEPTIONS
file_open_error = 1
file_read_error = 2
no_batch = 3
gui_refuse_filetransfer = 4
invalid_type = 5
no_authority = 6
unknown_error = 7
bad_data_format = 8
header_not_allowed = 9
separator_not_allowed = 10
header_too_long = 11
unknown_dp_error = 12
access_denied = 13
dp_out_of_memory = 14
disk_full = 15
dp_timeout = 16
OTHERS = 17.

IF sy-subrc EQ 0.

ENDIF.

Please note that the carriage return will not be visible when we view using ‘Notepad’. To view the carriage return, open the file using ‘Notepad++’ and click on the button ‘Show All Characters’ and the carriage return value will become visible.

After the file ‘test_file.txt’ (link) has been read by the function module ‘GUI_UPLOAD’, in debug we can notice that the internal table ‘GT_INPUT_FILE’ contains an additional character ‘#’ which represent the carriage return from the incoming file.

The character ‘#’ (from the incoming file) is not the same hash ‘#’ value that we type from the keyboard. They look similar but they both have different hexadecimal value.

If we move the hash from the incoming file to variable ‘GV_CARRIAGE_RETURN’ and move a normal hash ‘#’ (type from the keyboard) to variable ‘GV_HASH’. We can notice in debug that both look same but they have different hexadecimal values.

In case you want to identify the incoming hash ‘#’ using ABAP code, then we must use the attribute ‘CR_LF’ from the class interface ‘CL_ABAP_CHAR_UTILITIES’.

In debug mode, we can notice that the hexadecimal value of the attribute ‘CL_ABAP_CHAR_UTILITIES=>CR_LF+0(1)’ is same as the hexadecimal value of the carriage return from the incoming file.

For example:

The following code will return the carriage return hash ‘#’.

cl_abap_char_utilities=>cr_lf+0(1)

We can use the attribute ‘CR_LF+0 (1) to identify the carriage return hash ‘#’ from the incoming file using the following code:

Code:

IF cl_abap_char_utilities=>cr_lf+0(1) EQ gv_carriage_return.

MESSAGE \'This is a carriage return hash.\' TYPE \'I\'.

ENDIF.

Additional Information:

cl_abap_char_utilities=>cr_lf+0(1) : Return the carriage return

cl_abap_char_utilities=>cr_lf : Return the carriage return with line feed