fclose

Closes an open file.

Category: Data Import and Analysis > Data Import and Export

Syntax

status = fclose(fid)

Closes the file with identifier fid.

status = fclose('all')

Closes every file opened with fopen.

Description

Always close files you write to: data may sit in a buffer until the file is closed, and another program may not be able to open it meanwhile. After fclose, the identifier can no longer be used.

Input arguments

fid
File identifier returned by fopen.

Output arguments

status
0 on success, -1 if the identifier was not valid.

Example

Write, close, read

fid = fopen('c.txt', 'w');
fprintf(fid, 'saved\n');
status = fclose(fid)
disp(fileread('c.txt'))
Output
status = 0
saved

See also

fopen fprintf fread fileread