The ABAP File Interface

Overview

1. Opening files
2. Reading data
3. Writing data
4. Runtime errors

To use interfaces to other systems, the SAP system needs to be able to convert code pages when reading or writing to a file, or when performing data communication with other systems. The appropriate neue Sprachelemente new language elements have been available to make this possible.

1. Opening files

The OPEN DATASET dsn ... statement must include at least one of the following additions:

  1. ... IN TEXT MODE ENCODING ...
  2. ... IN BINARY MODE
  3. ... IN LEGACY TEXT MODE ...
  4. ... IN LEGACY BINARY MODE ...

If you use the TEXT MODE addition, you must also use an ENCODING addition.

In addition, you must include at least one of the following additions:

  1. ... FOR INPUT
  2. ... FOR OUTPUT
  3. ... FOR APPENDING
  4. ... FOR UPDATE

FOR INPUT opens a file for read access. This means that the system will return a runtime error if you try to change the file using TRANSFER.

The ... AT POSITION pos addition always specifies the position in bytes, regardless of whether the file was opened in TEXT MODE or BINARY MODE.

Notes

The system raises the exception CX_SY_FILE_OPEN if a file is opened, and then opened again without having been closed first. In earlier releases the file was often reopened to change the read/write position. You now use the SET DATASET to achieve this.

Your file names can also contain spaces in programs where Unicode checks are active.

2. Reading files

Before you use the READ DATASET dsn INTO f statement, you must open the file explicitly using a OPEN DATASET statement. Otherwise an exception that can be handled, CX_SY_FILE_OPEN_MODE, occurs.

If the file is opened in TEXT MODE, f must be a character-type field ( C, N, D, or T), a string, or a purely character-type structure. This check does not take place until runtime.

If the file is opened in TEXT MODE, LEGACY TEXT MODE, or LEGACY BINARY MODE, conversion errors may occur. If the system opens the file and the you have not declared the IGNORING CONVERSION ERRORS addition, the catchable runtime error CONVT_CODEPAGE occurs.

The ... [ ACTUAL ] LENGTH len und ... MAXIMUM LENGTH len additions specify the length of the file in characters if the file was opened in TEXT MODE, or in bytes otherwise.



3. Writing files

Before you use the TRANSFER f TO dsn statement, you must open the file explicitly using a OPEN DATASET ... statement. Otherwise an exception that can be handled,CX_SY_FILE_OPEN_MODE, occurs.

If the file has been opened in a Unicode-Programm with the FOR INPUT addition, you cannot write to it. A TRANSFER causes an exception that can be handled, CX_SY_FILE_OPEN_MODE.

If the file is opened in TEXT MODE, f must be a character-type field ( C, N, D, or T), a string, or a purely character-type structure. This check does not take place until runtime.

If the file is opened in TEXT MODE, LEGACY TEXT MODE, or LEGACY BINARY MODE, conversion errors may occur. If the system opens the file and the you have not declared the IGNORING CONVERSION ERRORS addition, an exception that can be handled, CX_SY_CONVERSION_CODEPAGE occurs.

The ...LENGTH len addition specifies the length of the file in characters if the file was opened in TEXT MODE, or in bytes otherwise.



4. Non-Catchable Exceptions

CX_SY_FILE_OPEN Error opening file
CX_SY_FILE_OPEN_MODE File opened in wrong mode
CX_SY_CONVERSION_CODEPAGE Conversion error