SAP NetWeaver AS ABAP Release 740, ©Copyright 2014 SAP AG. All rights reserved.
ABAP Keyword Documentation → ABAP − Reference → Processing External Data → ABAP File Interface → Statements for the ABAP File Interface → OPEN DATASET →
OPEN DATASET - mode
Syntax
... {BINARY MODE}
| {TEXT MODE encoding
[linefeed] }
| {LEGACY BINARY MODE [endian] [CODE PAGE cp]}
| {LEGACY TEXT MODE
[endian]
[CODE PAGE cp] [linefeed]} ... .
Alternatives:
1. ... BINARY MODE
2. ... TEXT MODE
3. ... LEGACY BINARY MODE
4. ... LEGACY TEXT MODE
Effect
These additions define whether the file is handled as a binary file or as a text file. By specifying LEGACY, files can be written in the format that is expected by a non-
Unicode system, and files that have been created by a non-Unicode-system can be read. The
byte order or the
code page must be specified explicitly. In
Unicode programs, you must specify the storage type.
... BINARY MODE
Effect
The addition IN BINARY MODE opens the file as a binary file. When writing to a binary file, the binary content of a data object is passed in unchanged form to the file. When reading from a binary file, the binary content of the file is passed in unchanged form to a data object.
The addition BINARY MODE has the same meaning in
Unicode programs and
non-Unicode programs.
... TEXT MODE
Effect
The addition IN TEXT MODE opens the file as a text file. The addition ENCODING defines how the characters are represented in the text file. When writing to a text file, the content of a data object is converted to the representation entered after ENCODING, and passed to the file. If the data type is character-like and flat, any trailing blanks are truncated. In the data type string, trailing blanks are not truncated. The end-of-line selection of the relevant platform is applied to the passed data by default. When reading from a text file, the content of the file is read until the next end-of-line selection, converted from the format specified after ENCODING into the current character format, and passed to a data object. The end-of-line selection used is controlled using the addition linefeed.
In Unicode programs,
only the content of character-like data objects can be passed to text files and read from text files.
The addition encoding must be specified in Unicode programs, and can only be omitted in non-Unicode programs.
... LEGACY BINARY MODE
Effect
Opening a legacy file.
The addition IN LEGACY BINARY MODE opens the file as a legacy binary file, where endian can be used to specify the
byte
order and CODE PAGE can be used to specify the
code page that handle the content of the file.
Note
When a flat character-like field is written to the legacy binary files, the number of bytes written
to the file is the same as the number of characters in the source field. In Unicode systems, the field
content can be influenced by this when writing texts in Eastern Asian languages. We therefore recommend only writing texts that were opened without the addition LEGACY.
... LEGACY TEXT MODE
Effect
Opening a legacy file. The addition IN LEGACY TEXT MODE opens the file as a legacy text file. In this case, as with legacy binary files, both the byte order and the code page used to handle the content of the file can be specified. The syntax and meaning of { BIG|LITTLE} ENDIAN, CODE PAGE cp, and linefeed are the same as for legacy binary files. The syntax and meaning of linefeed are the same as for regular text files.
In contrast to legacy binary files, the trailing blanks are truncated when writing character-like
flat data objects to a legacy text file. Also, as in the case of a text file, an
end-of-line selection is appended to the passed
data by default. In contrast to text files opened with the addition IN TEXT MODE,
there is no check in Unicode programs as to whether the data objects used in writing or reading are
character-like. Also, the LENGTH additions of the statements READ
DATASET and TRANSFER are used for counting. In legacy text files these
additions count in bytes, and an text files they count in the units of a character represented in the memory.
Notes
Example
A file test.dat is created as a text file, then filled with data, changed, and read. Since each TRANSFER statement appends an end-of-line selection to the written content, the content of the file is double-lined after the change. The first line contains "12ABCD". The second line contains "890". The character "7" was overwritten by the end-of-line selection of the first line.
DATA: file TYPE string VALUE `test.dat`,
result TYPE string.
DATA output TYPE TABLE OF string WITH EMPTY KEY.
OPEN DATASET file FOR OUTPUT IN TEXT MODE
ENCODING DEFAULT
WITH SMART LINEFEED.
TRANSFER `1234567890` TO file.
CLOSE DATASET file.
OPEN DATASET file FOR UPDATE IN TEXT MODE
ENCODING DEFAULT
WITH SMART LINEFEED
AT POSITION 2.
TRANSFER `ABCD` TO file.
CLOSE DATASET file.
OPEN DATASET file FOR INPUT IN TEXT MODE
ENCODING DEFAULT
WITH SMART LINEFEED.
WHILE sy-subrc = 0.
READ DATASET file INTO result.
APPEND result TO output.
ENDWHILE.
CLOSE DATASET file.
cl_demo_output=>display_data( output ).