ABAP OPEN DATASET statement for open a file

OPEN DATASET statement in ABAP is used for opening a presentation server file in the program. For closing the file CLOSE DATASET statement is used.

 

Syntax:

OPEN DATASET <dsn>

[FOR INPUT|OUTPUT|APPENDING|UPDATE]

[IN BINARY MODE |IN TEXT MODE | IN LEGACY BINARY MODE |IN                                                            LEGACY TEXT]

[REPLACEMENT CHARACTER <rc>]

[IGNORING CONVERSION ERRORS]

[AT POSITION <pos>]

[TYPE <c>]

[MESSAGE <mess>]

[FILTER <filt>].

 

Without any addition the OPEN DATASET statement open a file for reading purpose in binary mode. For other purposes we need to specify the statement with the proper additions.

 

See more details about each additions used with this statement

FOR OUTPUT: Open a file for writing

  • Will create a new file, if the file not exists
  • If the file is already opened, then the writing position set at the starting of the file
  • If the file is not opened already, the contents of the file will be deleted.
  • In this mode writing always start from the beginning of the file.

FOR INPUT: Open an existing file for writing.

Only the difference with OUTPUT addition is, it won’t create new file if it not exists.

FOR APPENDING: Open a file for writing at the end of file.

  • Will create a new file, if the file not exists
  • If the file is already opened, then the writing position set at the end of the file
  • In this mode writing always start from the end of the file.

IN BINARY MODE: This addition opens the file as a binary file. In this mode, the read and write operation doesn’t require interpreting the file contents, the entire data is used in an unchanged form.

IN TEXT MODE: This addition opens the file as a text file. In this mode, read and write operations deal the contents of the file in a line by line way. The addition ENCODING also used with for specifying the character representation for the file.

AT POSITION: This addition is used for specifying an exact position in bytes to read or write. For an example AT POSITION 10 will skip first 10 bytes in the file and the initial read or write operation will start from this position only.

TYPE: This addition is used for specifying further file attributes for passing to the operating system.

MESSAGE: This addition is used for storing the system messages like error messages at the time of opening files.

FILTER: This addition is for specifying the system commands like compress in UNIX.

 

It always a good practice to use CLOSE DATASET after the processing with the file opened with OPEN DATASET.

Also Read : View Other ABAP Keywords & Syntax -> ABAP Transaction codes