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

Close dataset

Former Member
0 Likes
2,310

Hi all,

what will happen if you did n't write CLOSE DATASET at the end of the file transfer.

Thanks in advance

srikanth

5 REPLIES 5
Read only

Former Member
0 Likes
1,283

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 successfully.

Read only

anversha_s
Active Contributor
0 Likes
1,283

hi,

<u>CLOSE FILE:</u>

The program will close all sequential files, which are open at the end of the program.

However, it is a good programming practice to explicitly close all the datasets that were opened.

Syntax:

CLOSE DATASET <file name>.

SY-SUBRC will be set to 0 or 8 depending on whether the CLOSE is successful or not.

Regards

Anver

Read only

Former Member
0 Likes
1,283

if the dataset is not closed...and when u run once again the program it gives error. it tries to open the dataset but as already it is open it gives error.

so it is good practice to always use close dataset.

Read only

0 Likes
1,283

what will happen if I am using same OPEN DATASET statement with out closing the previous file. will it survives or it will through an error

Read only

Former Member
0 Likes
1,283

Hi srikanth,

CLOSE DATASET <dsn>.

This statement closes the file <dsn>. The naming convention is described in the section Opening a File.

<b>You only need to close a file if you want to delete its contents the next time you open it for write access. </b>

Note

<b>However, to avoid errors, and to make your programs easier to read, you should always close a file before the next OPEN DATASET statement. The CLOSE statement divides your program into logical blocks, and makes them easier to maintain.</b>

Example

DATA FNAME(60) VALUE 'myfile'.

OPEN DATASET FNAME FOR OUTPUT.

.....

CLOSE FNAME.

OPEN DATASET FNAME FOR INPUT.

.....

CLOSE FNAME.

OPEN DATASET FNAME FOR INPUT AT POSITION <pos>.

.....

CLOSE FNAME.

<b>The CLOSE statement is not obligatory in this example,</b> but it does improve the layout of the program.