Application Development and Automation 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 only

OPEN file

Former Member
0 Likes
1,305

Hi.

Please give me simple code to create a file using <b>open dataset</b> statement .

and please explain.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,260

file gets stored in application server.To view it, go to transaction AL11.

Yo will find DIR/TEMP.Double click on it.in the next page you can see the file

11 REPLIES 11
Read only

Former Member
0 Likes
1,260

hi,

Check this sample code

OPEN DATASET

'D:\usr\sap\II3\DVEBMGS00\work\file1.txt'

FOR APPENDING IN TEXT MODE

ENCODING DEFAULT.

Read only

Manohar2u
Active Contributor
Read only

Former Member
0 Likes
1,260

Hi Prajwal,

LOOP at i_files.

CONCATENATE p_dir

i_files-name

INTO myfilename separated by '/'.

CONDENSE myfilename NO-GAPS.

OPEN DATASET myfilename FOR INPUT

MESSAGE l_msg

IN text MODE ENCODING DEFAULT.

F_RCODE = sy-subrc.

IF F_RCODE <> 0.

*Error handling

regards,

Sai

Read only

varma_narayana
Active Contributor
0 Likes
1,260

Hi Prajwal..

This is the Example code:

PARAMETERS: P_FILE(30) DEFAULT 'Mat.txt'.

Data : begin of It_Mat occurs 0,

Matnr type mara-matnr,

Mtart type mara-mtart,

End of IT_Mat.

Start-of-selection.

Select matnr mtart from mara

into table it_mat.

**To write the data in file

Open dataset p_file for OUPUT

IN TEXT MODE encoding default.

if sy-subrc <> 0.

exit.

endif.

Loop at it_mat.

Transfer it_mat to p_file.

endloop.

Close dataset p_file.

    • To read the Data from file

Open dataset p_file for INPUT

IN TEXT MODE encoding default.

if sy-subrc <> 0.

exit.

endif.

do .

READ DATASET P_FILE INTO IT_MAT.

IF SY-SUBRC NE 0. "END OF FILE

EXIT.

ENDIF.

ENDDO.

CLOSE DATASET P_FILE.

<b>Reward if Helpful</b>

Read only

0 Likes
1,260

hi Narayana.

thanks for u r reply

Please tell me where exactly data is stored, am not geting output.

Read only

Former Member
0 Likes
1,260

&----


*& Report Z_DATASET_APPENDING *

*& *

&----


REPORT z_dataset_appending .

&----


*& TYPES

&----


TYPES: BEGIN OF tw_mara,

matnr TYPE matnr, "Material Number

mtart TYPE mtart, "Material Type

meins TYPE meins, "Base Unit of Measure

END OF tw_mara,

tt_mara TYPE STANDARD TABLE OF tw_mara.

&----


*& DATA

&----


DATA: lw_mara TYPE tw_mara,

lt_mara TYPE tt_mara.

DATA: gf_matnr TYPE mara-matnr.

&----


*& SELECTION-SCREEN

&----


  • Material Info

SELECTION-SCREEN BEGIN OF BLOCK bk1 WITH FRAME TITLE text-001.

SELECT-OPTIONS : so_matnr FOR gf_matnr.

SELECTION-SCREEN END OF BLOCK bk1.

  • File Details

SELECTION-SCREEN BEGIN OF BLOCK bk2 WITH FRAME TITLE text-002.

PARAMETERS: pa_fpath TYPE rlgrap-filename.

SELECTION-SCREEN END OF BLOCK bk2.

&----


*& START-OF-SELECTION

&----


START-OF-SELECTION.

  • Fetch data from MARA table

SELECT matnr mtart meins INTO TABLE lt_mara

FROM mara

WHERE matnr IN so_matnr.

  • If no data found for the given selection.

IF sy-subrc NE 0.

MESSAGE i000(00) WITH 'No data found'.

LEAVE TO LIST-PROCESSING.

ELSE.

  • Open the dataset

OPEN DATASET pa_fpath FOR APPENDING IN TEXT MODE ENCODING DEFAULT.

IF sy-subrc EQ 0.

LOOP AT lt_mara INTO lw_mara.

  • Write the data into the file

TRANSFER lw_mara TO pa_fpath.

ENDLOOP.

ELSE.

WRITE 😕 'Error while opening the file'.

ENDIF.

  • Close the dataset

CLOSE DATASET pa_fpath.

  • Open the dataset for reading the file.

OPEN DATASET pa_fpath FOR INPUT IN TEXT MODE ENCODING DEFAULT.

IF sy-subrc EQ 0.

  • Read data from the file existing in the App. Server

DO.

READ DATASET pa_fpath INTO lw_mara.

IF sy-subrc NE 0.

  • If no data found in File, then exit from DO.. ENDDO.

EXIT.

ENDIF.

WRITE:/ lw_mara.

ENDDO.

ELSE.

WRITE 😕 'Error while opening the file'.

ENDIF.

  • Close the dataset

CLOSE DATASET pa_fpath.

ENDIF.

&----


*& Report Z_DATASET_BINARY *

*& *

&----


REPORT Z_DATASET_BINARY .

&----


*& TYPES

&----


TYPES: BEGIN OF tw_mara,

matnr TYPE matnr, "Material Number

mtart TYPE mtart, "Material Type

meins TYPE meins, "Base Unit of Measure

END OF tw_mara,

tt_mara TYPE STANDARD TABLE OF tw_mara.

&----


*& DATA

&----


DATA: lw_mara TYPE tw_mara,

lt_mara TYPE tt_mara.

DATA: gf_matnr TYPE mara-matnr.

&----


*& SELECTION-SCREEN

&----


  • Material Info

SELECTION-SCREEN BEGIN OF BLOCK bk1 WITH FRAME TITLE text-001.

SELECT-OPTIONS : so_matnr FOR gf_matnr.

SELECTION-SCREEN END OF BLOCK bk1.

  • File Details

SELECTION-SCREEN BEGIN OF BLOCK bk2 WITH FRAME TITLE text-002.

PARAMETERS: pa_fpath TYPE rlgrap-filename.

SELECTION-SCREEN END OF BLOCK bk2.

&----


*& START-OF-SELECTION

&----


START-OF-SELECTION.

  • Fetch data from MARA table

SELECT matnr mtart meins INTO TABLE lt_mara

FROM mara

WHERE matnr IN so_matnr.

  • If no data found for the given selection.

IF sy-subrc NE 0.

MESSAGE i000(00) WITH 'No data found'.

LEAVE TO LIST-PROCESSING.

ELSE.

  • Open the dataset in Binary Mode.

  • If you read from or write to a file that is open in binary mode, the

  • data is transferred byte by byte.

OPEN DATASET pa_fpath FOR OUTPUT IN BINARY MODE.

IF sy-subrc EQ 0.

LOOP AT lt_mara INTO lw_mara.

  • Write the data into the file

TRANSFER lw_mara TO pa_fpath.

ENDLOOP.

ELSE.

WRITE 😕 'Error while opening the file'.

ENDIF.

  • Close the dataset

CLOSE DATASET pa_fpath.

  • Open the dataset for reading the file.

OPEN DATASET pa_fpath FOR INPUT IN BINARY MODE.

IF sy-subrc EQ 0.

  • Read data from the file existing in the App. Server

DO.

READ DATASET pa_fpath INTO lw_mara.

IF sy-subrc NE 0.

  • If no data found in File, then exit from DO.. ENDDO.

EXIT.

ENDIF.

WRITE:/ lw_mara.

ENDDO.

ELSE.

WRITE 😕 'Error while opening the file'.

ENDIF.

  • Close the dataset

CLOSE DATASET pa_fpath.

ENDIF.

&----


*& Report Z_DATASET_READ *

*& *

&----


REPORT z_dataset_read .

&----


*& TYPES

&----


TYPES: BEGIN OF tw_mara,

matnr TYPE matnr, "Material Number

mtart TYPE mtart, "Material Type

meins TYPE meins, "Base Unit of Measure

END OF tw_mara,

tt_mara TYPE STANDARD TABLE OF tw_mara.

&----


*& DATA

&----


DATA: lw_mara TYPE tw_mara,

lt_mara TYPE tt_mara.

&----


*& SELECTION-SCREEN

&----


  • File Details

SELECTION-SCREEN BEGIN OF BLOCK bk1 WITH FRAME TITLE text-001.

PARAMETERS: pa_fpath TYPE rlgrap-filename.

SELECTION-SCREEN END OF BLOCK bk1.

&----


*& START-OF-SELECTION

&----


START-OF-SELECTION.

  • Open the dataset for reading the file.

OPEN DATASET pa_fpath FOR INPUT IN TEXT MODE ENCODING DEFAULT.

IF sy-subrc EQ 0.

  • Read data from the file existing in the App. Server

DO.

READ DATASET pa_fpath INTO lw_mara.

IF sy-subrc NE 0.

  • If no data found in File, then exit from DO.. ENDDO.

EXIT.

ENDIF.

WRITE:/ lw_mara.

ENDDO.

ELSE.

WRITE 😕 'Error while opening the file'.

ENDIF.

  • Close the dataset

CLOSE DATASET pa_fpath.

REWARD IF HELPFUL

Read only

Former Member
0 Likes
1,260

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.


Open DATASET dsn FOR OUTPUT in BINARY MODE ENCODING DEFAULT.

Reward points if useful else getbk,

Aleem.

Read only

0 Likes
1,260

hi

thanks to all for u r reply...... Please tell me where exactly file is stored.

and what is the difference b/w<b> open and download FM</b>

Regards

Prajwal.

Read only

Former Member
0 Likes
1,260

Hi Prajwal,

Using open dataset you can either create a file in the application server or read a file from application server.

for creating a file following is the syntax,

<b>data:filename(1024) type c value 'APPLFILE.TXT'</b><b>open dataset filename for output in text mode encoding default</b>.

This command creates a file in the application server with name APPLFILE.TXT.If a file already exists with the same name,it will be overwritten.

Now consider internal table i_error has certain data and yo need to upload it to application server.Following is the code.

<b>DATA : l_filestring TYPE string.

CLASS cl_abap_char_utilities DEFINITION LOAD.

CONSTANTS: con_tab TYPE c VALUE cl_abap_char_utilities=>horizontal_tab.

OPEN DATASET filename FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.

LOOP AT i_error.

CONCATENATE

i_error-type i_error-bptext

i_error-name_first i_error-distrib

i_error-pricing

i_error-address1 i_error-address2

i_error-address3 i_error-city

i_error-state i_error-post_code1

i_error-country i_error-phone1

i_error-phone2 i_error-contact_per

i_error-contact_mob i_error-email

i_error-fax1 i_error-fax2

i_error-birth i_error-arnnum

i_error-arndate i_error-pannum

i_error-brk_code i_error-sel

i_error-valid i_error-credat

i_error-recno i_error-bp

i_error-cp i_error-bp_flag

i_error-cp_flag i_error-iden_flag

i_error-iden_flag1 i_error-iden_flag2

i_error-attr_flag i_error-rel_flag

i_error-addr_flag i_error-message

INTO l_filestring SEPARATED BY con_tab.

TRANSFER l_filestring TO filename.

CLEAR l_filestring.

ENDLOOP.

CLOSE DATASET filename.</b>

The above code will create a tab delimited file in the application server.

Read only

Former Member
0 Likes
1,261

file gets stored in application server.To view it, go to transaction AL11.

Yo will find DIR/TEMP.Double click on it.in the next page you can see the file

Read only

Former Member
0 Likes
1,260

hi,

The output is stored in the Application Server.

The TCode for it AL11.

Goto AL11 and give the path which u opened and check the filename and u can find ur output there.