TRUNCATE DATASET

Short Reference

Syntax

TRUNCATE DATASET dset AT {CURRENT POSITION}|{POSITION pos}.

Extras:

1. ... CURRENT POSITION

2. ... POSITION pos

Effect

This statement sets the end of file of the file specified in dset to the value specified after AT and can thus change the size of the file. When shortened, the file is truncated after the new end of file; when extended, the file from the previous to the new end of file is filled with hexadecimal 0.

For dset, a character-type data object is expected which contains the platform-specific name of the file. The file must be opened for writing, adding, or changing, and not contain the FILTER addition to the OPEN DATASET statement, otherwise a non-catchable exception will result.

System fields

The statement always sets sy-subrc to the value 0 or triggers an exception.

Note

The TRUNCATE statement does not change the position of the current file pointer. If the file is open for adding, the file pointer is only set prior to the next write access to the end of file.

Addition 1

... CURRENT POSITION

Effect

With the CURRENT POSITION addition, the end of file is set to the current file pointer.

Addition 2

... POSITION pos

Effect

With the POSITION pos addition, the end of file is set to the position specified in pos. For pos, a numeric data object is expected whose contents cannot be negative. The positioning is specified in bytes, where the start of file is synonymous with the position 0.

Example

After the first TRUNCATE statement, the file contains the value "FF", and after the second, the value "FF00".

DATA: name TYPE string VALUE `test.dat`,
      hex  TYPE xstring.

hex = 'FFFF'.

OPEN DATASET name FOR OUTPUT IN BINARY MODE.

TRANSFER hex TO name.
SET DATASET name POSITION 0.
READ DATASET name INTO hex.

TRUNCATE DATASET name AT POSITION 1.
SET DATASET name POSITION 0.
READ DATASET name INTO hex.

TRUNCATE DATASET name AT POSITION 2.
SET DATASET name POSITION 0.
READ DATASET name INTO hex.

CLOSE DATASET name.

Exceptions

Catchable Exceptions

CX_SY_FILE_OPEN

CX_SY_FILE_AUTHORITY

CX_SY_FILE_POSITION

CX_SY_FILE_TRUNCATE

Non-Catchable Exceptions