ABAP Keyword Documentation → ABAP − Reference → Processing External Data → ABAP File Interface → Statements for the ABAP File Interface
TRUNCATE DATASET
Other versions: 7.31 | 7.40 | 7.54
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.
dset
expects a character-like data object containing the
physical name of
the file. The file must be opened for writing, appending, or changing, and not contain the addition
FILTER
of the
statement OPEN DATASET
; otherwise a non-handleable exception is raised.
Return Value
The statement TRUNCATE
always sets sy-subrc
to the value 0 or raises an exception.
Note
The statement TRUNCATE
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
The addition CURRENT POSITION
sets the end of file to the current file pointer.
Example
Truncates a file to the current position of the file pointer after reading five lines from a total of ten lines. When all lines are next read, only the first five lines are found.
DATA(dset) = 'test.dat'.
OPEN DATASET dset FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
DO 10 TIMES.
DATA(text) = |Entry { sy-index } |.
TRANSFER text TO dset.
ENDDO.
...
SET DATASET dset POSITION 0.
DO 5 TIMES.
READ DATASET dset INTO text.
ENDDO.
TRUNCATE DATASET dset AT CURRENT POSITION.
CLOSE DATASET dset.
...
OPEN DATASET dset FOR INPUT IN TEXT MODE ENCODING DEFAULT.
DATA(output) = ``.
WHILE sy-subrc = 0.
READ DATASET dset INTO text.
output &&= text.
ENDWHILE.
CLOSE DATASET dset.
cl_demo_output=>display( output ).
DELETE DATASET dset.
Addition 2
... POSITION pos
Effect
The addition POSITION pos
sets the end of file to the position specified
in pos
. pos
expects a numeric data object 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
Handleable Exceptions
CX_SY_FILE_OPEN
-
Cause: The file is not open.
Runtime error:DATASET_NOT_OPEN
-
Cause: The file is only open for reading.
Runtime error:DATASET_READ_ONLY
CX_SY_FILE_AUTHORITY
-
Cause: No authorization for access to file
Runtime error:OPEN_DATASET_NO_AUTHORITY
CX_SY_FILE_POSITION
-
Cause: Invalid position specified.
Runtime error:DATASET_OFFSET_TOO_LARGE
CX_SY_FILE_TRUNCATE
-
Cause: The operating system could not change the size of the file.
Runtime error:DATASET_TRUNCATE_ERROR
-
Cause: An attempt was made to change the size of a file opened using the addition
FILTER
.
Runtime error:DATASET_TRUNCATE_ERROR
Non-Handleable Exceptions
-
Cause: Internal error when emptying the file buffer, determining the current file position, or restoring the file when rolling in the
internal session.
Runtime error:DATASET_TRUNCATE_ERROR