Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Read the file from application server

Former Member
0 Kudos

Hi

How to read the file from application server using open dataset read data set

can u send me with syntax please ? its urgent

1 ACCEPTED SOLUTION

Former Member
5 REPLIES 5

Former Member

Former Member
0 Kudos

<b>OPEN DATASET</b>

Syntax

OPEN DATASET dset FOR access IN mode [position]

[os_addition]

[error_handling].

Effect

This statement opens the file specified in dset for the access specified in access in a storage mode specified in mode. For dset, a character-type data object is expected, which contains the platform-specific name of the file.

Use the additions position, os_addition, and error_handling to determine the position at which file is opened, specify platform-specific additions, and influence error handling.

In Unicode programs, the access and storage modes access and mode must be specified explicitly. If the additions are missing in non-Unicode programs, the file is opened implicitly as a binary file for read access.

In Unicode programs, the file must not yet be open in the current program; otherwise a treatable exception occurs. In non-Unicode programs, the file may already be open. The statement OPEN DATASET then does not reopen the file but moves the read or write position depending on the access mode. In this case, you should not change the access or storage mode.

Note

You can open up to 100 files per internal session. The actual maximum number of simultaneously open files may be less, depending on the platform.

Return Value

sy-subrc Description

0 File was opened.

8 Operating system could not open file.

<b>

TRANSFER</b>

Syntax

TRANSFER dobj TO dset [LENGTH len]

[NO END OF LINE].

<b>READ DATASET</b>

Syntax

READ DATASET dset INTO dobj [MAXIMUM LENGTH mlen]

[[ACTUAL] LENGTH alen].

Extras:

1. ... MAXIMUM LENGTH mlen

2. ... [ACTUAL] LENGTH alen

Effect

This statement exports data from the file specified in dset into the data object dobj. For dobj, variables with elementary data types and flat structures can be specified. In Unicode programs, dobj must be character-type if the file was opened as a text file.

For dset, a character-type data object is expected - that is, an object that contains the platform-specific name of the file. The content is read from the file starting from the current file pointer. After the data transfer, the file pointer is positioned after the section that was read. Using the MAXIMUM LENGTH addition, the number of characters or bytes to be read from the file can be limited. Using ACTUAL LENGTH, the number of characters or bytes actually used can be determined.

In a Unicode program, the file must be opened with an arbitrary access type; otherwise, an exception that cannot be handled will be triggered.

If the file has not yet been opened in anon-Unicode program, it will be implicitly opened as a binary file for read access using the statement

OPEN DATASET dset FOR INPUT IN BINARY MODE.

. If a non-existing file is accessed, an exception that can be handled can be triggered.

Influence of Access Type

Files can be read independently of the access type. Whether data can be read or not depends solely on the position of the file pointer. If the latter is at the end of the file or after the file, no data can be read and sy-subrc will be set to 4.

Influence of the Storage Type

The import function will take place irrespective of the storage type in which the file was opened with the statement OPEN DATASET.

If the file was opened as a text file or as a legacy text file, the data is normally read from the current position of the file pointer to the next end-of-line marking, and the file pointer is positioned after the end-of-line marking. If the data object dobj is too short for the number of read characters, the superfluous characters and bytes are cut off. If it is longer, it will be filled with blanks to the right.

If the file was opened as a binary file or as a legacy-binary file, as much data is read that fits into the data object dobj. If the data object dobj is longer than the number of exported characters, it is filled with hexadecimal 0 on the right.

If the specified storage type makes conversion necessary, this is executed before the assignment to the data object dobj. Afterwards, the read data is placed, byte by byte, into the data object.

System Fields

sy-subrc Meaning

0 Data was read without reaching end of file.

4 Data was read and the end of the file was reached or there was an attempt to read after the end of the file.

Note

The data from the text files should be imported solely into character-type data objects and data from binary files should be imported solely into byte-type data objects. To evaluate imported data as numeric data objects or mixed structures, it is recommended that you export these into binary containers and then assign these using the CASTING addition to the ASSIGN statement in accrodance with the typed field symbols. If the file is opened as a legacy-text file when such data is being imported, there is the danger that an end-of-line marking is contained in the binary representation of a number and that the numbe can therefore not be read.

Example

Importing the binary file flights.dat written from the example of the TRANSFER statement The data is written binary into a byte-type , typed field symbol <( <)>. Through the assignment of the structured data area wa to the field symbol, this adopts the length of the data area and a corresponding number of bytes for the loop process are imported. It would be possible to import directly into the structure wa with the same result, but the use of the field symbol is the recommended procedure. The reason is that in this way data is explicitly transferred from a binary file into a binary data type.

DATA: file TYPE string VALUE `flights.dat`,

wa TYPE spfli.

FIELD-SYMBOLS <hex_container> TYPE x.

OPEN DATASET file FOR INPUT IN BINARY MODE.

ASSIGN wa TO <hex_container> CASTING.

DO.

READ DATASET file INTO <hex_container>.

IF sy-subrc = 0.

WRITE: / wa-carrid,

wa-connid,

wa-countryfr,

wa-cityfrom,

wa-cityto,

wa-fltime,

wa-distance.

ELSE.

EXIT.

ENDIF.

ENDDO.

CLOSE DATASET file.

Addition 1

... MAXIMUM LENGTH mlen

Effect

This addition determines how many characters or how many bytes maximum are read from the file. For mlen, a data object of the type i is expected. It contains the number of characters or bytes. In the case of text files, the content of mlen determines how many characters are read from the file. In the case of binary, legacy-text, and legacy-binary files, mlen determines how many bytes are read from the file.

The first mlen characters or bytes are read from the current position of the file pointer and the file pointer is positioned after the read file. If the file is opened as a (legacy) text file and there isan end-of-line marking within the specified length, data is read only upto this position and the file pointer is positioned after the end-of-line marking.

If the value of mlen is the same as 0, no data is read. If the value of mlen is negative, the addition will be ignored and importing takes place in the same way as described for Influence of Storage Type.

Note

In case of text files, the number of bytes read depends on the character presentation specified using ENCODING when opening the file.

Example

This program section has the same functions as the previous example. Here data is imported - not into a byte-type field symbol, but into a byte-type data object hex_container. The number of bytes to be imported is determined by the typed field symbol <spfli>. This symbol is used in each loop process to access the imported data component by component.

DATA: file TYPE string VALUE `flights.dat`,

hex_container TYPE x LENGTH 1000,

len TYPE i.

FIELD-SYMBOLS <spfli> TYPE spfli.

DESCRIBE FIELD <spfli> LENGTH len IN BYTE MODE.

OPEN DATASET file FOR INPUT IN BINARY MODE.

ASSIGN hex_container TO <spfli> CASTING.

DO.

READ DATASET file INTO hex_container MAXIMUM LENGTH len.

IF sy-subrc = 0.

WRITE: / <spfli>-carrid,

<spfli>-connid,

<spfli>-countryfr,

<spfli>-cityfrom,

<spfli>-cityto,

<spfli>-fltime,

<spfli>-distance.

ELSE.

EXIT.

ENDIF.

ENDDO.

CLOSE DATASET file.

Addition 2

... [ACTUAL] LENGTH alen

Effect

This addition assigns the number of characters or bytes to be read from the file to the data object alen.

For alen, a variable of the type i is expected. For textfiles, the system determines how many characters were written to the memory area. With binary, legacy-text, and legacy-binary files, the system determines how many bytes were read from the file.

Note

Regardless of the length of the target field, the number of characters or bytes actually read from the file is always returned.

The optional addition ACTUAL was introduced for Release 6.10 in order to be better able to distinguish the addition LENGTH - which was also available in Releases prior to 6.10 - from the addition MAXIMUM LENGTH. As of Release 6.10, the addition ACTUAL should always be used.

Extras:

1. ... LENGTH len

2. ... NO END OF LINE

Effect

This statement passes the content of data object dobj to the file specified in dset. For dobj, you can specify data objects with elementary data types and flat structures. In Unicode programs, dobj must be character-type if the file was opened as a text file (this restriction does not apply to legacy text files).

dset is expected to be a character-type data object that contains the platform-specific name of the file. The content is written to the file from the current file pointer. After the transfer, the file pointer is positioned after the data that was added. You can use addition LENGTH to restrict the number of characters or bytes transferred

In a Unicode program, the file for writing, appending, or changing must be open. Otherwise, a treatable exception occurs.

If the file was not yet opened in a non-Unicode programm, it is implicitly opened using the statement

OPEN DATASET dset FOR OUTPUT IN BINARY MODE.

as a binary file for writing. If the system accesses an invalid file, a treatable exception is raised.

The Influence of Access Type

The access type defined in statement OPEN DATASET has the following effect on the transfer:

A file opened for reading using FOR INPUT cannot be written in Unicode programs. In non-Unicode programs, TRANSFER writes in a file opened for reading using FOR INPUT in exactly the same way as a file openedfor changing using FOR UPDATE.

In a file opened for writing using FOROUTPUT, the system writes to the file from the current file pointer. If the file pointer is positioned after the current start of the file, the file is pre-filled with hexadecimal 0 from the start of the file to the file pointer.

In a file opened for appending using FOR APPENDING, the system writes to the file from the current file pointer, which is always at the end of the file.

In a file opened for changing using FOR UPDATE, the system writes to the file from the current file pointer. If the file pointer is positioned after the end of the file, the file is pre-filled with hexadecimal 0 between the end of the file and the file pointer position.

Note

Prior to release 6.10: If parts of a file were to be overwritten, it was only possible to write to a file opened for reading. This is not allowed in Unicode programs. As of release 6.10 and later, you can open a file for changing which is the recommended procedure also for non-Unicode programs.

The Influence of Storage Type

The transfer depends on the storage type used when the file is opened using the statement OPEN DATASET. If the specified storage type requires conversion, it is carried out before the write process.

If the file was opened as a text file or a legacy text file, the trailing blank characters are deleted for all data objects, except for those of data type string. The line end marker defined when the file was opened is then added to the remaining content of the data object or to the result of the conversion, and the final result is written byte-by-byte to the file.

As of release 6.40, the appending of the end of line separator can be prevented using NO END OF LINE.

If the file was opened as a binary file or a legacy binary file, the content of the data object or the result of the conversion is written byte-by-byte to the file.

Note

Only character-type data objects should be written to text files. Only byte-type data objects should be written to binary files. To store numerical data objects or mixed structures, we recommend that you assign them to character-type or byte-type typed field symbols using the CASTING addition of the statement ASSIGN and save these field symbols.

Addition 1

... LENGTH len

Effect

This addition determines how many characters or how many bytes of data object dobj are written to the file. len is expected to be a data object of type i that contains the number of characters or bytes. In text files, the content of len specifies the number of characters that are written from the storage. For binary files, legacy text file, and legacy binary files, len specifies the number of bytes that are written to the file. The first len characters or bytes are transferred and alignment gaps are included in the structures. If the addition LENGTH is not specified, all characters or bytes are transferred.

If the value of len is less than or equal to 0, no characters or bytes are transferred. If the file is opened as a (legacy) text file, however, a line end marker is inserted into the file by default. If the value of len is greater than the number of characters or bytes in dobj, hexadecimal 0 or blank characters are transferred to the file instead of the missing bytes or characters, depending on whether the file was opened as a (legacy) text file or a (legacy) binary file.

Addition 2

... NO END OF LINE

Effect

This addition has the effect that no end of line separator is appended to the data transferred in text files or legacy text files.

Example

The binary data from database table SPFLI is transferred to a binary file flights.dat. The structure of the table rows transferred contains both character-type and numerical fields. Since the type-specific storage of mixed structures in files is not possible, the binary content of the structure is directly accessed using a typed field symbol . To attain the same result, you can directly transfer the structure wa, the recommended procedure is to use the field symbol, however, because it explicitly transfers a binary data type to a binary file. This type of storage is only recommended for short-term storage within the same system, because the byte-type content depends on the byte sequence and the current system code page. For long-term storage or for exchanging between systems, the data should be converted to character-type containers and stored as a text file.

DATA: file TYPE string VALUE `flights.dat`,

wa TYPE spfli.

FIELD-SYMBOLS TYPE x.

OPEN DATASET file FOR OUTPUT IN BINARY MODE.

SELECT *

FROM spfli

INTO wa.

ASSIGN wa TO CASTING.

TRANSFER TO file.

ENDSELECT.

CLOSE DATASET file.

<b>CLOSE DATASET</b>

Syntax

CLOSE DATASET dset.

Effect

This statement closes the file specified in dset. dset must be character-type data object that contains the platform-specific name of the file. If the file is already closed or does not exist, the statement is ignored and the return value sy-subrc is set to 0.

If the operating system buffers data before it is written to a file and there is still data in the buffer, this data is written to the file before closing.

Note

An opened file that was not explicitly closed using CLOSE DATASET is automatically closed when the program is exited.

System fields

If a file was opened without the FILTER addition, sy-subrc always contains the value 0.

If a file was opened using the FILTER addition, sy-subrc contains the return value of the filter program, which is returned by the operating system. This value is generally 0 if the statement was executed with no exceptions.

Former Member
0 Kudos

hi ramesh,

**--- Name of file inapplication server

fnm = 'bdcdata.txt'.

OPEN DATASET fnm FOR OUTPUT IN TEXT MODE encoding default. IF sy-subrc <> 0.

WRITE: / 'Error opening file'.

ENDIF.

**---Transfering data from internal table to dataset

LOOP AT i_mat INTO wa_mat.

TRANSFER wa_mat TO fname.

ENDLOOP.

CLOSE DATASET fname.

**-----Opening dataset for reading

OPEN DATASET fnm FOR INPUT IN TEXT MODE encoding default.

        • Reading the file from application server

DO.

READ DATASET fnm INTO wa_string.

IF sy-subrc <> 0.

EXIT.

ENDIF.

  • Can put ur code here and use the work area wa_string.

CLOSE DATASET fname.

<b>Hope this is helpful, Do reward.</b>

Former Member
0 Kudos

Ramesh,

File manuplations...............................

1. At the presentation level:

->GUI_UPLOAD

->GUI_DOWNLOAD

->CL_GUI_FRONTEND

2. At the application server level:

->OPEN DATASET : open a file in the application server for reading or writing.

->READ DATASET : used to read from a file on the application server that has been opened for reading

-> TRANSFER DATASET : writing data to a file.

-> CLOSE DATASET : closes the file

-> DELETE DATASET : delete file

<b>reward points if useful.</b>

regards,

Vinod Samuel.

varma_narayana
Active Contributor
0 Kudos

Hi

Sample code:

DATA: V_RECORD(200).

OPEN DATASET P_FILE FOR INPUT IN TEXT MODE ENCODING DEFAULT.

IF SY-SUBRC <> 0.

EXIT.

ENDIF.

DO.

READ DATASET P_FILE INTO V_RECORD.

IF SY-SUBRC NE 0.

EXIT.

ENDIF.

SPLIT V_Record at CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB "Tab delimeter

INTO WA-FIELD1 WA-FIELD2.

APPEND WA TO ITAB.

ENDDO.

Reward if HElpful.