OPEN DATASET 

OPEN


1. ... FOR OUTPUT
2. ... FOR INPUT
3. ... FOR APPENDING
4. ... IN BINARY MODE
5. ... IN TEXT MODE
6. ... AT POSITION pos
7. ... TYPE ctrl
8. ... MESSAGE mess
9. ... FILTER f

Effect

Opens the specified file. If no addition is specified, the file is opened for reading and in binary mode (see below).

Example

DATA: DSN(20) VALUE '/usr/test', 
      RECORD(80). 
 
OPEN DATASET DSN. 
DO. 
  READ DATASET DSN INTO RECORD. 
  IF SY-SUBRC NE 0. 
    EXIT. 
  ELSE. 
    WRITE: / RECORD. 
  ENDIF. 
ENDDO. 
CLOSE DATASET DSN.

The file must be accessible from the application server. You cannot use OPEN DATASET to process files on the current presentation server (whether PC or workstation). The function modules WS_DOWNLOAD and WS_UPLOAD exist for this purpose.

The following catchable runtime errors may occur with this statement:

Effect

Opens the file for writing. If the file already exists, its contents are deleted unless it is already open. If it is open, the positioning is set back to the start of the file. If the file does not exist, it is created.

Effect

Opens an existing file for writing. If the file is already open, the positioning is set back to the start of the file. The addition FOR INPUT does not have to be specified explicitly.

Effect

Opens the file for writing to the end of the file. If the file does not exist, it is created. If the file is already open, the positioning is set to the end.

Effect

The contents of the file are not interpreted by the read and write operations READ DATASET and TRANSFER. The data areas specified with these key words are directly input or output. The addition IN BINARY MODE does not have to be specified explicitly.

Effect

If a file is opened with this addition, the system assumes that the file has a line structure. Each time READ DATASET or TRANSFER occurs, exactly one line is input or output. If the data area is too big for the line read, the remaining area is padded with blanks. If it is too small, the remainder of the line is lost.

Effect

This addition allows you to specify an explicit file position pos in bytes from the start of the file. The next read or write operation then occurs there. You cannot specify a position before the start of the file.
Although this addition can also be used with the addition IN TEXT MODE , it makes little sense, because the physical format of a text file depends largely on the operating system.

Effect

In the field ctrl, you can specify further file attributes. The contents of this field are passed unchanged to the operating system. No checks are performed. See the documentation of the fopen command for the relevant operating system.

Example

create an MVS file "'QXX.YYY'" with the specified attributes. (The apostrophes ['] are part of the file name.):

OPEN DATASET '''QXX.YYY''' 
  TYPE 'lrecl=80, blksize=8000, recfm=F' 
  FOR OUTPUT. 

Example

Create an OpenVMS file 'TEST.LOG' with the specified attributes. The individual parameters must be separated by blanks:

OPEN DATASET 'TEST.LOG' 
  TYPE 'alq=100 deq=100 fop=cif,ctg' 
  FOR OUTPUT. 

Effect

Stores the relevant operating system message in the field mess if an error occurs when opening the file.

Example

DATA: DSN(20) VALUE '/usr/test', 
      MSG(100). 
 
OPEN DATASET DSN FOR INPUT MESSAGE MSG. 
 
IF SY-SUBRC <> 0. 
  WRITE: / MSG. 
  STOP. 
ENDIF.

Effect

Under UNIX and Windows NT, you can specify an operating system command in the field f.

DATA DSN(20) VALUE '/usr/test.Z'.
OPEN DATASET DSN FOR OUTPUT FILTER 'compress'.

opens the file DSN and writes the output data to this file via the UNIX command 'compress'.

OPEN DATASET DSN FOR INPUT FILTER 'uncompress'.

reads the file in again.

Related

READ DATASET, TRANSFER, CLOSE DATASET