Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Processing External Data →  ABAP File Interface →  Statements for the ABAP File Interface 

OPEN DATASET

Quick Reference

Other versions: 7.31 | 7.40 | 7.54

Syntax


OPEN DATASET dset FOR access IN
mode [position] 
                                     [os_additions]
                                     [error_handling].

Effect

This statement opens the file specified in dset on the host computer of the current AS Instance for the access specified in access in a storage mode specified in mode. dset expects a character-like data object containing the physical name of the file. The file must not yet be open in the current program; otherwise a handleable exception is raised.

Use the additions positions, os_addition, and error_handling to determine the position at which file is opened, specify platform-specific additions, and influence error handling.


Notes

Security Note

Access to a file whose name dset is injected into a program from outside is a serious security risk. Any names passed to a program from outside must be checked thoroughly before being used. See Directory Traversal.

Return Value

sy-subrc Meaning
0 The file was opened.
8 The operating system could not open the file.


Example

The example demonstrates the following:

  • Opening a file for writes
  • Writing binary XML data to the file
  • Closing the open file
  • Opening the file for reads
  • Reading the binary XML data from the file
  • Closing the open file
  • Deleting the file

The XML data is created by transforming an internal table to the asXML format.

SELECT * 
     FROM scarr 
     INTO TABLE @DATA(itab). 
CALL TRANSFORMATION id SOURCE scarr = itab 
                      RESULT XML DATA(xml). 

DATA(dset) = 'scarr.dat'. 
OPEN DATASET dset FOR OUTPUT IN BINARY MODE. 
TRANSFER xml TO dset. 
CLOSE DATASET dset. 

... 

CLEAR xml. 
OPEN DATASET dset FOR INPUT IN BINARY MODE. 
READ DATASET dset INTO xml. 
CLOSE DATASET dset. 

CALL TRANSFORMATION id SOURCE XML xml 
                       RESULT scarr = itab. 
cl_demo_output=>display( itab ). 

DELETE DATASET dset. 

Exceptions

Handleable Exceptions

CX_SY_FILE_OPEN

  • Cause: The file is already open.
    Runtime error: DATASET_REOPEN

CX_SY_CODEPAGE_CONVERTER_INIT

  • Cause: The required conversion is not supported. (Due to specification of invalid code page or of language not supported in the conversion, with SET LOCALE LANGUAGE.)
    Runtime error: CONVT_CODEPAGE_INIT

CX_SY_CONVERSION_CODEPAGE

  • Cause: Internal error in the conversion.
    Runtime error: CONVT_CODEPAGE

CX_SY_FILE_AUTHORITY

  • Cause: No authorization for access to file
    Runtime error: OPEN_DATASET_NO_AUTHORITY
  • Cause: Authorization for access to this file is missing in OPEN DATASET with the addition FILTER.
    Runtime error: OPEN_PIPE_NO_AUTHORITY

CX_SY_PIPES_NOT_SUPPORTED

  • Cause: The operating system does not support pipes.
    Runtime error: DATASET_NO_PIPE

CX_SY_TOO_MANY_FILES

  • Cause: Maximum number of open files exceeded.
    Runtime error: DATASET_TOO_MANY_FILES

Non-Handleable Exceptions

  • Cause: An attempt was made to open a pipe that is already open.
    Runtime error: DATASET_PIPE_POSITION

Continue

OPEN DATASET - access

OPEN DATASET - mode

OPEN DATASET - position

OPEN DATASET - os_additions

OPEN DATASET - error_handling