Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

how to write read dataset statement in unicode

Former Member
0 Kudos

Hi All,

I am writing the program using open dataset concept .

i am using follwing code.

PERFORM FILE_OPEN_INPUT USING P_P_IFIL.

READ DATASET P_P_IFIL INTO V_WA.

IF SY-SUBRC <> 0.

V_ABORT = C_X.

WRITE: / TEXT-108.

PERFORM CLOSE_FILE USING P_P_IFIL.

ELSE.

V_HEADER_CT = V_HEADER_CT + 1.

ENDIF.

Read dataset will work for normal code.

when it comes to unicode it is going to dump.

Please can u tell the solution how to write read dataset in unicode.

Very urget.

Regards

Venu

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi Venu,

This example deals with the opening and closing of files.

Before Unicode conversion

data:

begin of STRUC,

F1 type c,

F2 type p,

end of STRUC,

DSN(30) type c value 'TEMPFILE'.

STRUC-F1 = 'X'.

STRUC-F2 = 42.

  • Write data to file

open dataset DSN in text mode. ß Unicode error

transfer STRUC to DSN.

close dataset DSN.

  • Read data from file

clear STRUC.

open dataset DSN in text mode. ß Unicode error

read dataset DSN into STRUC.

close dataset DSN.

write: / STRUC-F1, STRUC-F2.

This example program cannot be executed in Unicode for two reasons. Firstly, in Unicode programs, the file format must be specified more precisely for OPEN DATASET and, secondly, only purely character-type structures can still be written to text files.

Depending on whether the old file format still has to be read or whether it is possible to store the data in a new format, there are various possible conversion variants, two of which are introduced here.

After Unicode conversion

Case 1: New textual storage in UTF-8 format

....

data:

begin of STRUC2,

F1 type c,

F2(20) type c,

end of STRUC2.

  • Put data into text format

move-corresponding STRUC to STRUC2.

  • Write data to file

open dataset DSN in text mode for output encoding utf-8.

transfer STRUC2 to DSN.

close dataset DSN.

  • Read data from file

clear STRUC.

open dataset DSN in text mode for input encoding utf-8.

read dataset DSN into STRUC2.

close dataset DSN.

move-corresponding STRUC2 to STRUC.

write: / STRUC-F1, STRUC-F2.

The textual storage in UTF-8 format ensures that the created files are platform-independent.

After Unicode conversion

Case 2: Old non-Unicode format must be retained

...

  • Write data to file

open dataset DSN in legacy text mode for output.

transfer STRUC to DSN.

close dataset DSN.

  • read from file

clear STRUC.

open dataset DSN in legacy text mode for input.

read dataset DSN into STRUC.

close dataset DSN.

write: / STRUC-F1, STRUC-F2.

Using the LEGACY TEXT MODE ensures that the data is stored and read in the old non-Unicode format. In this mode, it is also possible to read or write non-character-type structures. However, be aware that data loss and conversion errors can occur in Unicode systems if there are characters in the structure that cannot be represented in the non-Unicode codepage.

Reward pts if found usefull 🙂

Regards

Sathish

3 REPLIES 3

Former Member
0 Kudos

Hi Venu,

This example deals with the opening and closing of files.

Before Unicode conversion

data:

begin of STRUC,

F1 type c,

F2 type p,

end of STRUC,

DSN(30) type c value 'TEMPFILE'.

STRUC-F1 = 'X'.

STRUC-F2 = 42.

  • Write data to file

open dataset DSN in text mode. ß Unicode error

transfer STRUC to DSN.

close dataset DSN.

  • Read data from file

clear STRUC.

open dataset DSN in text mode. ß Unicode error

read dataset DSN into STRUC.

close dataset DSN.

write: / STRUC-F1, STRUC-F2.

This example program cannot be executed in Unicode for two reasons. Firstly, in Unicode programs, the file format must be specified more precisely for OPEN DATASET and, secondly, only purely character-type structures can still be written to text files.

Depending on whether the old file format still has to be read or whether it is possible to store the data in a new format, there are various possible conversion variants, two of which are introduced here.

After Unicode conversion

Case 1: New textual storage in UTF-8 format

....

data:

begin of STRUC2,

F1 type c,

F2(20) type c,

end of STRUC2.

  • Put data into text format

move-corresponding STRUC to STRUC2.

  • Write data to file

open dataset DSN in text mode for output encoding utf-8.

transfer STRUC2 to DSN.

close dataset DSN.

  • Read data from file

clear STRUC.

open dataset DSN in text mode for input encoding utf-8.

read dataset DSN into STRUC2.

close dataset DSN.

move-corresponding STRUC2 to STRUC.

write: / STRUC-F1, STRUC-F2.

The textual storage in UTF-8 format ensures that the created files are platform-independent.

After Unicode conversion

Case 2: Old non-Unicode format must be retained

...

  • Write data to file

open dataset DSN in legacy text mode for output.

transfer STRUC to DSN.

close dataset DSN.

  • read from file

clear STRUC.

open dataset DSN in legacy text mode for input.

read dataset DSN into STRUC.

close dataset DSN.

write: / STRUC-F1, STRUC-F2.

Using the LEGACY TEXT MODE ensures that the data is stored and read in the old non-Unicode format. In this mode, it is also possible to read or write non-character-type structures. However, be aware that data loss and conversion errors can occur in Unicode systems if there are characters in the structure that cannot be represented in the non-Unicode codepage.

Reward pts if found usefull 🙂

Regards

Sathish

Former Member
0 Kudos

you can read the dataset like this

Record should be of type char with lenght calculated from the structure in this case wa_eord. This is for a tab delimited file. if its not tab delimited the use the corresponding separator in the split statement.

DO.

READ DATASET p1 INTO record.

IF sy-subrc = 0.

SPLIT record AT cl_abap_char_utilities=>horizontal_tab

INTO wa_eord-matnr wa_eord-werks wa_eord-vdatu wa_eord-bdatu

wa_eord-lifnr wa_eord-flifn wa_eord-ebeln wa_eord-ebelp

wa_eord-febel wa_eord-reswk wa_eord-fresw wa_eord-ematn

wa_eord-notkz wa_eord-ekorg wa_eord-vrtyp wa_eord-eortp

wa_eord-autet wa_eord-meins wa_eord-sobkz.

APPEND wa_eord TO it_eord.

ELSE.

EXIT.

ENDIF.

ENDDO.

Thanks.

Message was edited by:

mg s

Message was edited by:

mg s

Former Member
0 Kudos

Venu,

To create an output file:

open dataset YOUR_FILENAME_HERE in text mode for output encoding default.

To read an input file:

open dataset YOUR_FILENAME_HERE in text mode for input encoding default.