OPEN DATASET dsn.
1a. ...
FOR INPUT
1b. ...
FOR OUTPUT
1c. ...
FOR APPENDING
1d. ...
FOR UPDATE
2a. ...
IN BINARY MODE
2b. ...
IN TEXT MODE [ENCODING (DEFAULT|UTF-8|NON-UNICODE)]
2c. ...
IN LEGACY BINARY MODE [(BIG|LITTLE) ENDIAN] [CODE PAGE cp]
2d. ...
IN LEGACY TEXT MODE [(BIG|LITTLE) ENDIAN] [CODE PAGE cp]
3. ...
REPLACEMENT CHARACTER rc
4.
... IGNORING CONVERSION ERRORS
5.
... AT POSITION p
6.
... TYPE c
7. ... MESSAGE m
8. ... FILTER f
In some cases, the syntax rules that apply to Unicode programs are different than those for non-Unicode programs. For details see the section File Interface.
Opens the specified file. If you do not specify a directory, the system uses the directory defined in the profile parameter DIR_HOME.
In programs without active Unicode check, the file is opened for reading in binary mode if you do not use any additions for OPENDATASET. To ensure downward compatibility with Releases <= 4.6, file names containing blanks are truncated at the position of the first blank.
In programs with active Unicode check, you must specify the access type (such as
... FOR INPUT, ... FOR
OUTPUT, and so on) and the mode (such as ... IN TEXT MODE,
... IN BINARY MODE, and so on). If the file is opened
using ... IN TEXT MODE, you must still use the addition
... ENCODING. If the Unicode check is enabled, it is possible
to use file names containing blanks. Applying OPEN DATASET
to a file already opened - in the same internal mode - triggers an exception of the type CX_SY_FILE_OPEN.
The Return Code is set as follows:
DATA:
dsn(20) TYPE C VALUE '/usr/test.dat',
rec(80) TYPE C.
OPEN
DATASET dsn FOR INPUT IN TEXT MODE ENCODING DEFAULT.
IF sy-subrc <> 0.
EXIT.
ENDIF.
READ DATASET dsn INTO rec.
WHILE sy-subrc <> 0.
WRITE / rec. READ DATASET dsn INTO rec.
ENDWHILE.
CLOSE DATASET dsn.
The system opens the file test.dat in the directory usr of the application server and reads it line by line.
Access rights to files:
When you create a file, it is created under the user name
used to start the SAP System. This is not usually the name of the current user. To allow the system
to create files, you must assign write authorization to the user name of the SAP System in the relevant directory.
... FOR INPUT
OPEN ... FOR INPUT opens the file in read mode.
If the file does not exist, OPEN ... FOR INPUT fails with Return Code SY-SUBRC = 8.
If OPEN DATASET is not executed in a Unicode
program and if the user has write authorization for the file, the file is opened in read and write mode. Otherwise, it is only opened in read mode.
... FOR OUTPUT
OPEN ... FOR OUTPUT opens the file in write mode.
If the file already exists, its existing content is deleted. If the file does not exist, the system creates it.
... FOR APPENDING
OPEN ... FOR APPENDING opens the file in append mode.
If the file already exists, its contents are retained, and the system moves to the end of the file.
If the file does not exist, the system creates it. If the file was already open, the system moves to the end of the file.
When you open a file using
FOR APPENDING, READDATASET
always returns Return Code SY-SUBRC = 4 which is used to display the end of the file.
... FOR UPDATE
OPEN ... FOR UPDATE opens the file in read and write mode.
If the file does not exist, OPEN ... FOR UPDATE fails
with Return Code SY-SUBRC = 8.
... IN BINARY MODE
Data is read or written unchanged (as stored in the memory). (For details, see
READ DATASET and TRANSFER.)
This file format is used if you do not specify a MODE addition.
... IN TEXT MODE [ENCODING (DEFAULT|UTF-8|NON-UNICODE)]
Data is read or written line by line. (For details, see
READ DATASET and TRANSFER.)
on ENCODING (DEFAULT|UTF-8|NON-UNICODE)
This addition specifies the character representation in the file:
... IN LEGACY BINARY MODE [(BIG|LITTLE) ENDIAN] [CODE PAGE cp]
Data is read or written in a form which is compatible to BINARY MODE
in Releases <= 4.6. This addition is primarily used to convert a file into the code page format specified
already when it is opened. At runtime, the system uses the format of the system code page of the application
server. The system saves the file then again in the code page specified. This procedure is important
if data is exchanged between systems using different code pages. For more information, see
READ DATASET and TRANSFER.
... IN LEGACY TEXT MODE [(BIG|LITTLE) ENDIAN] [CODE PAGE cp]
Data is read or written in a form which is compatible to BINARY
MODE in Releases <= 4.6. This addition is primarily used to convert a file into the code
page format specified already when it is opened. At runtime, the system uses the format of the system
code page of the application server. The system saves the file then again in the code page specified.
This procedure is important if data is exchanged between systems using different code pages. For more
information, see READ DATASET and TRANSFER.
on BIG ENDIAN, LITTLE ENDIAN
on CODE PAGE cp
... REPLACEMENT CHARACTER rc
Specifies the replacement character which is used when a character cannot be converted during a character set conversion.
This addition may only be used in combination with the additions IN
TEXT MODE and IN LEGACY ... MODE. If the addition
is not specified, "#" is used as the replacement character.
... IGNORING CONVERSION ERRORS
This addition ensures that no exception is triggered when an error occurs during character set conversion
and a file is accessed in read or write mode. If you do not use this addition, the exception CX_SY_CONVERSION_CODEPAGE
is triggered when a conversion error occurs. (For details see
READ DATASET and TRANSFER.)
... AT POSITION p
Sets the read/write position in the file to p. p is interpreted as the byte offset from the beginning of the file.
... TYPE c
You can specify additional file attributes in the field c.
The field contents are passed to the operating system without changes and without any checks for correctness.
For more information on the correctness of the attributes specified, refer to the open system call documentation of the relevant operating system.
If the application server runs under Windows NT and the file was opened using IN TEXT MODE or IN LEGACY TEXTMODE, the TYPE addition is interpreted as follows:
Generating the
OS/400 file test.dat
with the properties specified under TYPE. The following example specifies the record length, the block size, and the record format.
OPEN DATASET 'test.dat'
TYPE 'lrecl=80, blksize=8000, recfm=FB'
FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
... MESSAGE m
If an error occurs opening the file, the corresponding message of the operating system is stored in the field m.
DATA: dsn(20) VALUE '/usr/test.dat',
msg(100).
OPEN
DATASET dsn FOR INPUT MESSAGE msg
IN TEXT MODE ENCODING DEFAULT.
IF sy-subrc <> 0.
WRITE / msg.
STOP.
ENDIF.
... FILTER f
If you are working under UNIX or Windows NT, you can specify an operating system command in the field f.
Under UNIX, the following
DATA dsn(20) VALUE '/usr/test.dat'.
OPEN DATASET dsn FOR OUTPUT FILTER 'compress'
IN TEXT MODE ENCODING DEFAULT.
opens the file dsn and writes the data to the file in compressed form using the UNIX command compress.
OPEN DATASET dsn FOR INPUT FILTER 'uncompress'
IN TEXT MODE ENCODING DEFAULT.
reads the file again.
Catchable Exceptions
CX_SY_FILE_OPEN
CX_SY_CODEPAGE_CONVERTER_INIT
CX_SY_CONVERSION_CODEPAGE
CX_SY_FILE_AUTHORITY
CX_SY_PIPES_NOT_SUPPORTED
CX_SY_TOO_MANY_FILES
Non-Catchable Exceptions
End of line marking under Windows NT
Since under Windows NT the line end in text
files can be marked both with "
CRLF" and with "LF",
text files are handled in a special way on this operating system when opened using FOR OUTPUT or FOR APPENDING:
READ DATASET, TRANSFER,
CLOSE DATASET, GET
DATASET POSITION, DELETE DATASET
Opening Files